Given an m x n 2D grid where 1 represents land and 0 represents water, return the number of islands.
An island is surrounded by water and is formed by connecting adjacent land cells horizontally or vertically. You may assume all four edges of the grid are surrounded by water.
grid = [
[1, 1, 1, 1, 0],
[1, 1, 0, 1, 0],
[1, 1, 0, 0, 0],
[0, 0, 0, 0, 0]
]1grid = [
[1, 1, 0, 0, 0],
[1, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 1]
]3m == grid.lengthn == grid[i].length1 <= m, n <= 300grid[i][j] is 0 or 1Expected time complexity: O(m × n)Run your code to see results
Use Cmd+Enter to run