Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

1020. Number of Enclaves

Medium69.6% Acceptance
ArrayDepth-First SearchBreadth-First Search
Asked by:
Google
ProblemHints (2)Solutions (12)VideosCompanies (1)Notes

Problem Statement

You are given an m x n binary matrix grid, where 0 represents a sea cell and 1 represents a land cell.

A move consists of walking from one land cell to another adjacent (4-directionally) land cell or walking off the boundary of the grid.

Return the number of land cells in grid for which we cannot walk off the boundary of the grid in any number of moves.

Example 1:

Input: grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
Output: 3
Explanation: There are three 1s that are enclosed by 0s, and one 1 that is not enclosed because its on the boundary.

Example 2:

Input: grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
Output: 0
Explanation: All 1s are either on the boundary or can reach the boundary.

Constraints:

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

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
G

Approach

The goal of #1020 Number of Enclaves is to count the number of land cells in a grid that cannot reach the boundary by moving in four directions. A key insight is that any land cell connected to the boundary cannot be part of an enclave.

A common strategy is to start from the boundary cells and eliminate all land connected to the edges using Depth-First Search (DFS) or Breadth-First Search (BFS). By traversing from boundary land cells and marking them as visited or converting them to water, we effectively remove all non-enclave land. After this traversal, the remaining land cells in the grid represent enclaves.

An alternative approach uses Union Find (Disjoint Set) to group connected land cells and track whether a group touches the boundary. Only groups not connected to the border are counted.

Both traversal approaches process each cell at most once, resulting in efficient performance for typical matrix sizes.

Complexity

ApproachTime ComplexitySpace Complexity
DFS / BFS from BoundaryO(m × n)O(m × n) in worst case due to recursion/queue
Union FindO(m × n · α(m × n))O(m × n)

Video Solution Available

take U forward

View all video solutions

Problem Hints

Use these hints if you're stuck. Try solving on your own first.

1
Hint 1

Can you model this problem as a graph problem? Create n * m + 1 nodes where n * m nodes represents each cell of the map and one extra node to represent the exterior of the map.

2
Hint 2

In the map add edges between neighbors on land cells. And add edges between the exterior and land nodes which are in the boundary. Return as answer the number of nodes that are not reachable from the exterior node.

Ready to see the solutions?View Solutions

Solutions (12)

Approach 1: Depth-First Search (DFS)

This approach uses a depth-first search (DFS) to mark land cells connected to the boundaries. We iterate over the boundary cells of the grid and if a land cell is found, we perform DFS to mark all connected land cells as visited. Finally, we count all remaining unvisited land cells inside the grid, which are considered enclaves.

Time Complexity: O(m * n) as each cell is visited at most once.
Space Complexity: O(m * n) in the worst case due to recursion stack used by DFS.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2
3void dfs(int** grid, int m, int n, int i, int j) {
4    if

Explanation

This C code defines a function dfs that marks land cells ('1') as water ('0'), effectively marking them as visited. The main function numEnclaves iterates over the borders of the grid. For each unvisited land cell on the border, DFS is called to mark connected land. Finally, it counts all unvisited land cells inside the grid as enclaves.

Approach 2: Breadth-First Search (BFS)

In this approach, we use a queue to perform BFS to mark land cells connected to the boundaries. By enqueueing each boundary land cell and exploring its neighbors iteratively, we can mark all reachable boundary-connected lands. This is followed by counting the unvisited land cells – those are the enclaves.

Time Complexity: O(m * n).
Space Complexity: O(min(m, n)) considering the queue size in the worst case.

CC++JavaPythonC#JavaScript
1#





Video Solutions

Watch expert explanations and walkthroughs

G-15. Number of Enclaves | Multi-source BFS | C++ | Java

take U forward
15:34167,445 views

Asked By Companies

1 companies
G
Google

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Surrounded RegionsMedium
Number of IslandsMedium
Alien DictionaryHard
Smallest Rectangle Enclosing Black PixelsHard
More similar problems

Related Topics

ArrayDepth-First SearchBreadth-First SearchUnion FindMatrix

Problem Stats

Acceptance Rate69.6%
DifficultyMedium
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Number of Enclaves asked in FAANG interviews?

Yes, grid traversal problems like Number of Enclaves are common in FAANG-style interviews. They test understanding of DFS, BFS, graph connectivity, and matrix traversal techniques.

What is the optimal approach for Number of Enclaves?

The optimal approach is to start DFS or BFS from all boundary land cells and mark every reachable land cell. After removing these cells, the remaining land cells in the grid are enclaves. This method ensures each cell is processed at most once.

What data structure is best for solving Number of Enclaves?

Graph traversal structures like recursion stacks for DFS or queues for BFS work best. These help explore all connected land cells efficiently in the grid. Union Find can also be used to track connected components and detect boundary connections.

Why do we start traversal from boundary cells in Number of Enclaves?

Any land connected to the boundary can eventually reach the grid edge, so it cannot be an enclave. By marking all land reachable from the boundary first, we isolate only the cells fully surrounded by water.

(
i
<
0
||
j
<
0
||
i
>=
m
||
j
>=
n
||
grid
[
i
]
[
j
]
==
0
)
{
5
return
;
6
}
7
grid
[
i
]
[
j
]
=
0
;
// Marking visited land
8
dfs
(
grid
,
m
,
n
,
i
+
1
,
j
)
;
9
dfs
(
grid
,
m
,
n
,
i
-
1
,
j
)
;
10
dfs
(
grid
,
m
,
n
,
i
,
j
+
1
)
;
11
dfs
(
grid
,
m
,
n
,
i
,
j
-
1
)
;
12
}
13
14
int
numEnclaves
(
int
*
*
grid
,
int
gridSize
,
int
*
gridColSize
)
{
15
int
m
=
gridSize
;
16
int
n
=
*
gridColSize
;
17
for
(
int
i
=
0
;
i
<
m
;
i
++
)
{
18
if
(
grid
[
i
]
[
0
]
==
1
)
{
19
dfs
(
grid
,
m
,
n
,
i
,
0
)
;
20
}
21
if
(
grid
[
i
]
[
n
-
1
]
==
1
)
{
22
dfs
(
grid
,
m
,
n
,
i
,
n
-
1
)
;
23
}
24
}
25
for
(
int
j
=
0
;
j
<
n
;
j
++
)
{
26
if
(
grid
[
0
]
[
j
]
==
1
)
{
27
dfs
(
grid
,
m
,
n
,
0
,
j
)
;
28
}
29
if
(
grid
[
m
-
1
]
[
j
]
==
1
)
{
30
dfs
(
grid
,
m
,
n
,
m
-
1
,
j
)
;
31
}
32
}
33
int
count
=
0
;
34
for
(
int
i
=
0
;
i
<
m
;
i
++
)
{
35
for
(
int
j
=
0
;
j
<
n
;
j
++
)
{
36
if
(
grid
[
i
]
[
j
]
==
1
)
{
37
count
++
;
38
}
39
}
40
}
41
return
count
;
42
}
43
include
<stdio.h>
2
#
include
<stdlib.h>
3
4
typedef
struct
{
5
int
row
;
6
int
col
;
7
}
Point
;
8
9
int
*
*
directions
=
(
int
*
[
]
)
{
(
int
[
]
)
{
1
,
0
}
,
(
int
[
]
)
{
-
1
,
0
}
,
(
int
[
]
)
{
0
,
1
}
,
(
int
[
]
)
{
0
,
-
1
}
}
;
10
11
void
bfs
(
int
*
*
grid
,
int
m
,
int
n
,
int
startRow
,
int
startCol
)
{
12
Point
*
queue
=
(
Point
*
)
malloc
(
m
*
n
*
sizeof
(
Point
)
)
;
13
int
front
=
0
,
rear
=
0
;
14
queue
[
rear
++
]
=
(
Point
)
{
startRow
,
startCol
}
;
15
grid
[
startRow
]
[
startCol
]
=
0
;
16
while
(
front
<
rear
)
{
17
Point current
=
queue
[
front
++
]
;
18
for
(
int
d
=
0
;
d
<
4
;
d
++
)
{
19
int
newRow
=
current
.
row
+
directions
[
d
]
[
0
]
;
20
int
newCol
=
current
.
col
+
directions
[
d
]
[
1
]
;
21
if
(
newRow
>=
0
&&
newRow
<
m
&&
newCol
>=
0
&&
newCol
<
n
&&
grid
[
newRow
]
[
newCol
]
==
1
)
{
22
grid
[
newRow
]
[
newCol
]
=
0
;
23
queue
[
rear
++
]
=
(
Point
)
{
newRow
,
newCol
}
;
24
}
25
}
26
}
27
free
(
queue
)
;
28
}
29
30
int
numEnclaves
(
int
*
*
grid
,
int
gridSize
,
int
*
gridColSize
)
{
31
int
m
=
gridSize
,
n
=
*
gridColSize
;
32
33
for
(
int
i
=
0
;
i
<
m
;
i
++
)
{
34
if
(
grid
[
i
]
[
0
]
==
1
)
bfs
(
grid
,
m
,
n
,
i
,
0
)
;
35
if
(
grid
[
i
]
[
n
-
1
]
==
1
)
bfs
(
grid
,
m
,
n
,
i
,
n
-
1
)
;
36
}
37
for
(
int
j
=
0
;
j
<
n
;
j
++
)
{
38
if
(
grid
[
0
]
[
j
]
==
1
)
bfs
(
grid
,
m
,
n
,
0
,
j
)
;
39
if
(
grid
[
m
-
1
]
[
j
]
==
1
)
bfs
(
grid
,
m
,
n
,
m
-
1
,
j
)
;
40
}
41
42
int
count
=
0
;
43
for
(
int
i
=
0
;
i
<
m
;
i
++
)
{
44
for
(
int
j
=
0
;
j
<
n
;
j
++
)
{
45
if
(
grid
[
i
]
[
j
]
==
1
)
count
++
;
46
}
47
}
48
return
count
;
49
}
50

Explanation

This C implementation uses a BFS method where we use a queue to explore all land cells connected to the boundary. As the boundary is iterated, each connected land cell is marked by setting its value to zero (i.e., visited).