You are given an m x n binary grid where 1 represents land and 0 represents water.
An island is a group of 1s connected 4-directionally (horizontal or vertical). You may assume all four edges of the grid are surrounded by water.
The area of an island is the number of cells with value 1 in the island.
Return the maximum area of an island in the grid. If there is no island, return 0.
grid = [
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0],
[0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0]
]6grid = [[0, 0, 0, 0, 0, 0, 0, 0]]0m == grid.lengthn == grid[i].length1 <= m, n <= 50grid[i][j] is 0 or 1Expected time complexity: O(m × n)Run your code to see results
Use Cmd+Enter to run