Count the Lakes
chart is a rectangular map given as a list of strings of equal length. Each character is '~' for water or '#' for land.
Two water cells are joined when they touch along a side or at a corner, so each cell has up to eight neighbours. A lake is a maximal group of water cells in which every cell can be reached from every other through joined water cells. Water on the edge of the map is still part of a lake. Return the number of lakes; return 0 when there is no water.
Example 1
The three water cells in the top left touch along sides, and the bottom right cell touches the middle one at a corner, so all four form one lake.
Example 2
There is no water.
Constraints
1 <= len(chart) <= 101 <= len(chart[i]) <= 10, all rows the same length- Each character is
'~'or'#'
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.