Number of Closed Islands Problem
Description
LeetCode Problem 1254.
Given a 2D grid consists of 0s (land) and 1s (water). An island is a maximal 4-directionally connected group of 0s and a closed island is an island totally (all left, top, right, bottom) surrounded by 1s.
Return the number of closed islands.
Example 1:

Input: grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]]
Output: 2
Explanation:
- The grid contains four islands of 0s.
- The island spanning rows 1 to 3 and columns 1 to 4 is surrounded entirely by 1s. It also encloses the water cell at row 2, column 2. It is therefore closed.
- The single 0 at row 3, column 6 has 1s on all four sides, so it is also closed.
- The two remaining islands both lie in column 7, which is on the border of the grid, so neither of them is closed.
- Two islands are closed, shown in gray, so the answer is 2.
Example 2:

Input: grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]
Output: 1
Explanation:
- The grid contains three islands of 0s.
- The single 0 at row 1, column 2 has 1s above it, below it, to its left and to its right, so it is closed. It is shown in gray.
- The island formed by rows 0 to 2 of column 0 together with row 0, column 1 lies on the border, so it is not closed.
- The island formed by rows 0 to 2 of column 4 together with row 0, column 3 also lies on the border, so it is not closed.
- One island is closed, so the answer is 1.
Example 3:
Input: grid = [[1,1,1,1,1,1,1],[1,0,0,0,0,0,1],[1,0,1,1,1,0,1],[1,0,1,0,1,0,1],[1,0,1,1,1,0,1],[1,0,0,0,0,0,1],[1,1,1,1,1,1,1]]
Output: 2
Explanation:
- The grid contains two islands of 0s, one nested inside the other.
- The outer island is the ring of 0s occupying rows 1 to 5 and columns 1 to 5. It is enclosed by the outer border of 1s, so it is closed.
- The inner island is the single 0 at row 3, column 3. It is enclosed by the inner ring of 1s, so it is also closed.
- Both islands are closed, so the answer is 2.
Constraints:
1 <= grid.length, grid[0].length <= 1000 <= grid[i][j] <=1
Solution
Approach: Flood Fill Each Island
An island is closed exactly when none of its cells is on the border of the grid: a land cell on the border has the outside of the grid as a neighbor instead of water, and any island that does not reach the border is stopped by water on every side.
So we scan the grid. Each time we find a land cell (0) that we have not seen, we flood-fill its whole island with a stack (depth-first), turning every land cell we reach into water (1) so it is never visited again. While filling, we note whether any cell of the island lies in the first or last row or column. If none does, the island is closed and we count it.
The tricky part is to keep filling after we find a border cell. Stopping early would leave the rest of that island as land, and the scan would later count those leftover cells as a separate, possibly "closed", island. An explicit stack also keeps a large winding island (up to 104 cells) from overflowing the call stack.
Complexity: O(m·n) time, since each cell is pushed at most once, and O(m·n) space for the stack in the worst case.
class Solution:
def closedIsland(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
closed = 0
for i in range(m):
for j in range(n):
if grid[i][j] != 0:
continue
# Flood-fill this island, turning its land into water so each cell is visited once.
touches_border = False
grid[i][j] = 1
stack = [(i, j)]
while stack:
r, c = stack.pop()
if r == 0 or r == m - 1 or c == 0 or c == n - 1:
touches_border = True # keep filling: the rest of the island must be marked too
for x, y in ((r - 1, c), (r + 1, c), (r, c - 1), (r, c + 1)):
if 0 <= x < m and 0 <= y < n and grid[x][y] == 0:
grid[x][y] = 1
stack.append((x, y))
if not touches_border:
closed += 1
return closed