Number of Islands
Medium
LC #200
griddfsconnected componentsNot attempted yet
A map is a grid of "1" (land) and "0" (water).
An island is a group of land cells connected up,
down, left or right (not diagonally). Everything outside
the grid is water. Return the number of islands.
Example 1
Input: grid = [["1","1","0","0"],
["1","0","0","1"],
["0","0","1","1"]]
Output: 2
Example 2
Input: grid = [["1","0","1"],
["0","1","0"],
["1","0","1"]]
Output: 5
Diagonal neighbours don't join islands.
Constraints
1 <= rows, cols <= 200grid[r][c]is"0"or"1"- Big islands are tested: a recursive DFS may exceed Python's recursion limit.