Count islands on a grid
A grid is a graph in disguise: each cell is a node and its up, down, left and right neighbours are edges. Counting islands of land is counting connected components of land cells.
grid = ["LL.",
".L.",
"..L"]
# two islands: diagonal cells do not touchThink before coding
Scan every cell. When an unvisited land cell appears, count a new island and flood from it, marking every land cell you can reach. Check the bounds before indexing so the edges of the grid are safe. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
Write count_islands(grid), where grid is a list of equal-length strings with "L" for land and "." for water. Neighbours touch up, down, left or right.
The explanation opens here once every check passes.