← 返回 uber 的题目列表Number of Islands
类型:online_judge
Problem
Given an m x n 2D grid grid consisting of characters '1' (land) and '0' (water), return the number of islands.
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically (4-directional). You may assume all four edges of the grid are surrounded by water.
Input
The first line contains two integers m n.
The next m lines each contain a string of length n (only 0/1).
Output
Print one integer: the number of islands.
Constraints
1 <= m, n <= 300
grid[i][j] in {'0','1'}
Example
Input:
4 5
11000
11000
00100
00011
Output:
3
Example
Input
1 1
0
Output
0