You're exploring a mysterious archipelago represented by a binary matrix where 0 represents water and 1 represents land. Your goal is to find landlocked islands - pieces of land that are completely surrounded by water or other land, with no path to escape to the ocean.

Think of it this way: if you were standing on a land cell, you can move in 4 directions (up, down, left, right) to adjacent land cells. If there's any possible path that leads you to the boundary of the grid (the edge where you can "walk off" into the ocean), then that land is not an enclave.

Return the total number of land cells that form enclaves - land from which you cannot reach the boundary no matter how many moves you make.

Example: In a 4×4 grid, if there's a small island in the center completely surrounded by water or boundary-connected land, those center cells would be enclaves.

Input & Output

example_1.py — Basic Island
$ Input: grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
Output: 3
💡 Note: There are three enclaves: the isolated '1' cells that cannot reach any boundary. The cells at positions (1,0) and (1,2) can potentially reach boundaries, but (2,1) and (2,2) form an enclave that cannot.
example_2.py — All Connected
$ Input: grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
Output: 0
💡 Note: All land cells are connected to the boundary through the top-right corner land cell, so there are no enclaves. Every '1' can trace a path to the edge.
example_3.py — Complete Enclave
$ Input: grid = [[0,0,0,0,0],[0,1,1,1,0],[0,1,0,1,0],[0,1,1,1,0],[0,0,0,0,0]]
Output: 8
💡 Note: The entire inner island is surrounded by water and cannot reach any boundary. All 8 land cells form one large enclave completely isolated from the edges.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m, n ≤ 500
  • grid[i][j] is either 0 or 1

Visualization

Tap to expand
Number of Enclaves - DFS Approach INPUT Binary Matrix (4x4) 0 0 0 0 1 0 1 0 0 1 1 0 0 0 0 0 Water (0) Boundary Land Interior Land [[0,0,0,0],[1,0,1,0], [0,1,1,0],[0,0,0,0]] ALGORITHM STEPS 1 Find Boundary Land Scan all 4 edges of grid 2 DFS from Boundaries Mark reachable land cells 3 Flood Fill Connected Visit all 4-directional neighbors 4 Count Enclaves Count unmarked land cells After DFS Marking: X X = Marked (reachable) FINAL RESULT Enclave Cells Identified -- 1 1 1 Enclave (landlocked) Boundary-connected Output: 3 3 enclave land cells Key Insight: Instead of checking if each land cell can reach the boundary (expensive), we flip the problem: Start DFS from all boundary land cells and mark everything reachable. Remaining unmarked land = enclaves. Time: O(m*n) | Space: O(m*n) for recursion stack TutorialsPoint - Number of Enclaves | DFS Approach
Asked in
Google 23 Amazon 18 Microsoft 15 Meta 12
42.0K Views
Medium Frequency
~15 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen