← 返回 bytedance 的题目列表Number of Islands
类型:online_judge
Problem: Number of Islands
Given a 2D grid grid consisting of characters '1' representing land and '0' representing water, count the number of islands.
An island is formed by connecting adjacent land cells horizontally or vertically. Diagonal connections do not count. You may assume all four edges of the grid are surrounded by water.
Return the total number of islands.
Input Format
The first line contains two integers m and n, the number of rows and columns.
The next m lines each contain a string of length n, consisting only of characters '0' and '1'.
Output Format
Print one integer: the number of islands.
Constraints
1 <= m, n <= 300
grid[i][j] is either '0' or '1'
Example
Input:
4 5
11110
11010
11000
00000
Output:
1
Example
Input
4 5
11110
11010
11000
00000
Output
1