A graph does not have to be in one piece. Each group of nodes joined to one another is a component; on a grid these are islands. To count them you go through every cell in turn: when you meet a land cell not seen yet, you have found a new island; you start a walk there and mark the whole of it.
islands = 0seen = set()for r in range(len(grid)):for c in range(len(grid[0])):if grid[r][c] == "." and (r, c) not in seen:islands += 1reachable(grid, r, c, seen)return islands
If you want the island's size you count the cells the walk paints: the difference in the set's size before and after, or a number the walk returns. For all the nested loops, the total work is the number of cells, because each cell is painted only once.