2Brute2Best
🔥0

Number of Enclaves

Medium·LeetCode #1020 Pro OnlyMedium
LeetCode
01

Problem Statement

You are given a grid of 0s (sea) and 1s (land). A move walks from a land cell to a neighbouring land cell (up, down, left or right), or walks off the edge of the grid. Return how many land cells you cannot walk off the edge from, in any number of moves. Those land cells are enclosed.

Input

grid, an m × n grid of 0s and 1s.

Output

The number of land cells that can never reach the edge of the grid.

Example 1
Input: grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
Output: 3

Three of the 1s are enclosed by 0s. The 1 at the left edge can walk off the grid, so it is not counted.

Example 2
Input: grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
Output: 0

Every 1 is on the edge, or can walk to the edge through other 1s, so none is enclosed.

Constraints
  • • 1 ≤ m, n ≤ 500, where m is the number of rows and n is the number of columns
  • • grid[i][j] is 0 or 1
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“cannot walk off the edge”
→ The question is about reaching the border, which is an easy start for a flood.
“walks up, down, left or right”
→ The flood moves four ways, the same as in Number of Islands.
“how many land cells”
→ Count cells, not islands: the answer is the number of 1s left after the flood.
03

Approach Complexity Ladder

Brute Force — a search from every land cellO((m · n)²)O(m · n)

For each land cell, run a fresh search; count the cell if the search never reaches the edge

Optimal — flood from the border, then count what is leftO(m · n)O(m · n)

Sink every land cell connected to the border; the land still standing is enclosed

Brute Force Intuition

For each land cell, run a search over land to see whether it can reach a border cell. If none of the land it reaches is on the border, count the cell. An island with k cells is searched k times, so the same answer is found again and again.

Recognition rule for this sub-pattern:

Start a DFS/BFS from each unvisited cell; everything it reaches is one component.
Remember this · Graph BFS / DFS
Trigger
Grid of cells, islands, regions, "minimum steps", transformations between states, connected components.
Think
What is a node, what is an edge, and do I need the shortest path or just reachability?
Key question
How do I mark visited so I never process a node twice?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Graph BFS / DFS translate into production architectures and low-level memory mechanics.