Problem. Given an R × C grid of "1" (land) and "0" (water), return the number of islands: groups of land cells
connected horizontally or vertically.
1 1 0 0 0
1 1 0 0 0 → 3
0 0 1 0 0
0 0 0 1 1
Lesson 11.2 filled one connected region. Counting all of them is the core connected components problem: every unvisited land cell found by a scan starts a new island, and a flood fill claims the whole island at once.
ML context. After a segmentation model predicts a binary mask, the first post-processing step is usually connected
components: how many objects (cells in a microscope image, cars in a road scene, lesions in a scan), how big each is, and
removing specks below a minimum area. scipy.ndimage.label and OpenCV's connectedComponents implement exactly this
(Changes A and C).