The Number of Closed Islands problem gives you a binary matrix where 0 represents land and 1 represents water. A "closed island" is a group of 0-cells completely surrounded by 1-cells — it must not touch the boundary. Count the number of closed islands. This Number of Closed Islands coding problem uses DFS or BFS with boundary flood-fill to eliminate non-closed islands.
Microsoft, Meta, Amazon, Google, and Bloomberg ask this to test the "boundary flood-fill before counting" technique. Candidates must eliminate all land connected to the matrix boundary (those can't be closed) and then count remaining land groups. The array, matrix, union find, BFS, and DFS interview pattern is the core.
Boundary flood-fill + island counting. Step 1: DFS/BFS from all boundary 0-cells, marking them as visited (turning them to 1). Step 2: Count remaining connected groups of 0-cells — each is a closed island. This two-pass approach cleanly separates non-closed (boundary-connected) from closed islands.
Matrix:
1 1 1 1 1
1 0 0 0 1
1 0 1 0 1
1 0 0 0 1
1 1 1 1 1
All boundary cells are 1. Interior 0-cells form one connected group. Not connected to boundary → 1 closed island.
Matrix with boundary land: flood-fill those first, then count remaining.
"Count interior enclosed regions" problems always require boundary elimination first. The pattern: flood-fill from all boundary cells of the target type, then count remaining groups. This same technique applies to "number of enclaves," "number of closed rooms," and similar grid containment problems. Practice the boundary-first DFS template until it's automatic — it appears across many matrix exploration problems.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Maximum Number of Fish in a Grid | Medium | Solve | |
| Number of Enclaves | Medium | Solve | |
| Detect Cycles in 2D Grid | Medium | Solve | |
| Count Sub Islands | Medium | Solve | |
| Surrounded Regions | Medium | Solve |