← 返回 bytedance 的题目列表Count Islands and Return the Maximum Island Size
类型:online_judge
Given a 2D binary grid grid:
1 represents land.
0 represents water.
Land cells connected horizontally or vertically belong to the same island. Diagonal adjacency does not connect islands.
Return two values:
The total number of islands.
The maximum area among all islands, where an island's area is its number of land cells.
If the grid contains no land, the maximum area is 0.
Input Format
First line: two integers m n, the number of rows and columns.
Next m lines: each contains n characters, each being 0 or 1.
Output Format
Print two integers: number_of_islands maximum_island_area.
Constraints
1 <= m, n <= 500
grid[i][j] is either 0 or 1.
Use iterative DFS with an explicit stack to avoid recursion-depth issues.
Example
Input:
4 5
11000
11000
00100
00011
Output:
3 4
Example
Input
4 5
11000
11000
00100
00011
Output
3 4