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
๐Ÿ๏ธ Island Enclave DetectionOriginal Grid:011001100000After Boundary DFS:0XX001X00000Legend:Original LandBoundary-ConnectedEnclave (Answer)๐Ÿ๏ธ Blue = Water, Green = Original Land๐Ÿ”ฅ Red X = Marked as boundary-connected๐Ÿ† Yellow = Enclave (count = 1)Algorithm: Start from boundary โ†’ Mark reachable โ†’ Count remaining
Understanding the Visualization
1
Identify Coast
Find all land cells on the grid boundary - these are your starting points
2
Coastal Exploration
From each coastal land, explore all connected land using DFS
3
Mark Reachable
Mark or remove all land that can be reached from the coast
4
Count Isolated
Count remaining unmarked land cells - these are true enclaves
Key Takeaway
๐ŸŽฏ Key Insight: Instead of asking 'Can this cell reach the boundary?', ask 'Which cells CAN'T be reached FROM the boundary?' This reverse approach is much more efficient!
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