Count Land Islands
Problem statement
For this exercise, assume grid is a rectangular map: 1 is land and 0 is water. An island is a maximal group of land cells connected by shared sides (up, down, left or right). Diagonals do not connect islands.
Return the number of islands. Do not change the input. A grid with no cells has zero islands.
Function
countIslands(grid: int[][]) → intExamples
Example 1
grid = [[1,1,0,0],[0,1,0,1],[1,0,0,1]]return = 3The upper-left group, lower-left cell and right-hand group are three islands.
Example 2
grid = [[1,0],[0,1]]return = 2The two land cells touch only diagonally.
Example 3
grid = []return = 0The map has no rows.
Constraints
0 <= grid.length <= 60.- All rows have the same length
c, with0 <= c <= 60. - Every cell is
0or1.