How do you count the number of islands in a grid?
Scan every cell. When you find a land cell that has not been visited, you have found a new island: increment the counter and flood fill from that cell, marking every land cell connected to it as visited. The flood fill does not need a separate visited array if you write over the land with water as you go.
The key insight is that an island is exactly one connected component of land cells under four-directional adjacency, so the problem is a connected component count on a grid.
The mechanism is that each flood fill consumes exactly one whole island. By the time the scan moves on, no cell of that island can trigger another fill, so the counter increments once per island and never twice for the same land mass.
The trade-off to name is the recursion depth of the flood fill. A recursive fill nests one frame per cell in the island, so a large island can exceed the Python recursion limit, and the iterative version with an explicit stack avoids that. The other detail worth stating is the direction set: most versions of this problem use up, down, left and right, and diagonal connectivity is a different problem with a different answer, so the direction set has to be agreed before coding. Marking cells as visited by changing their value is a nice trick, but it means the function mutates its input, which you have to say out loud.