Number of Islands
Problem statement
You are given an m x n grid of characters. A cell containing '1' is land and a cell containing '0' is water.
An island is a maximal group of land cells connected horizontally or vertically. Treat the area outside all four grid edges as water.
Return the number of islands in the grid.
Function
numIslands(grid: char[][]) → intExamples
Example 1
grid = [["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]return = 1Every land cell belongs to one four-directionally connected component.
Example 2
grid = [["1","1","0","0","0"],["1","1","0","0","0"],["0","0","1","0","0"],["0","0","0","1","1"]]return = 3The grid contains three separate land components.
Constraints
1 <= m, n <= 300.m == grid.lengthandn == grid[i].length.- Every cell is either
'0'or'1'.