Question
You are given a rectangular binary grid. `1` represents land and `0` represents water. Cells belong to the same island when they share an edge; diagonal contact does not connect islands. First implement: ```python def count_islands(grid: list[list[int]]) -> int: ... ``` Then support a grid that begins as all water. After each `(row, column)` operation, turn that cell into land and report the current island count: ```python def island_counts_after_adds(rows: int, cols: int, adds: list[tuple[int, int]]) -> list[int]: ... ``` Adding an already-land cell leaves the count unchanged. Assume coordinates are in bounds. ## Staged follow-ups 1. Compare DFS, BFS, and disjoint-set solutions for the static problem. 2. Achieve near-constant amortized work per addition with union by rank and path compression. 3. Discuss the harder case in which land can also be removed.