Module 8 · Graphs and Trees · BFS on Grid

Number of islands

Scan the grid cell by cell. Land nobody has visited yet means a new island: a BFS from there visits all of it, so the rest of that island is skipped by the scan. The number of BFS runs is the number of islands.