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: Islands in gray are closed because they are completely surrounded by water (group of 1s).
Example 2:

Input: grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]] Output: 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
Constraints:
1 <= grid.length, grid[0].length <= 1000 <= grid[i][j] <=1The grid can be viewed as a graph where each cell is a node connected to its four possible neighbors. To identify closed islands, we can use a DFS approach starting from any cell containing 0 (cell of land). This DFS should mark the entire island by changing those 0s to a visited flag, such as -1. During the traversal, if any part of the island reaches the grid's boundary, it cannot be a closed island.
For each unvisited 0, if it forms a complete island (i.e., it does not reach the boundary), we increment the closed island count. We examine cells systematically, ensuring that all possible islands are accounted for.
The C solution uses a recursive DFS function called dfs. For each cell containing 0, it triggers a DFS which marks the visited islands with -1. The boolean flag isClosed helps determine if the island touches the boundary.
C++
Java
Python
C#
JavaScript
Time Complexity: O(m * n), where m is the number of rows, and n is the number of columns. Each cell is visited once.
Space Complexity: O(m * n) due to the recursion stack.
This approach employs BFS to traverse the islands. By leveraging a queue, we can iteratively explore all 0-connected land cells starting from each unvisited land cell. If during any BFS iteration, we find ourselves at the boundary or edge of the grid, the ongoing island is not closed.
Using BFS here can make iterative exploration more intuitive and can be preferred in languages where managing stacks explicitly is a common challenge, although the core logic of edge-checking remains similar.
The C solution employs a manual queue for BFS due to language constraints. The grid modifies in place, leveraging BFS to spread through each island starting from unvisited land cells.
C++
Java
Python
C#
JavaScript
Time Complexity: O(m * n), where m is the number of rows, and n is the number of columns.
Space Complexity: O(m + n) due to the queue memory usage during BFS.
| Approach | Complexity |
|---|---|
| Depth-First Search (DFS) with Edge Checking | Time Complexity: O(m * n), where |
| Breadth-First Search (BFS) with Queue | Time Complexity: O(m * n), where |
LeetCode Number of Islands Solution Explained - Java • Nick White • 531,028 views views
Watch 9 more video solutions →Practice Number of Closed Islands with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor