C Program
#include <stdio.h>
int g[4][5] = {
{1,1,0,0,0},
{1,1,0,0,1},
{0,0,0,1,1},
{0,0,0,0,0}
}, r=4, c=5;
void dfs(int x, int y) {
if(x<0||y<0||x>=r||y>=c||g[x][y]==0) return;
g[x][y]=0;
dfs(x+1,y); dfs(x-1,y); dfs(x,y+1); dfs(x,y-1);
}
int main() {
int count=0;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
if(g[i][j]) { dfs(i,j); count++; }
printf("Number of Islands: %d", count);
}C Output
Input: 1 1 0 0 0 1 1 0 0 1 0 0 0 1 1 0 0 0 0 0 Output: Number of Islands: 2
C++ Program
#include <iostream>
using namespace std;
int g[3][5] = {
{1,1,0,0,0},
{0,1,0,0,1},
{1,0,0,1,1}
};
void dfs(int x, int y) {
if(x<0||y<0||x>=3||y>=5||g[x][y]==0) return;
g[x][y]=0;
dfs(x+1,y); dfs(x-1,y); dfs(x,y+1); dfs(x,y-1);
}
int main() {
int count=0;
for(int i=0;i<3;i++)
for(int j=0;j<5;j++)
if(g[i][j]) { dfs(i,j); count++; }
cout << "Number of Islands: " << count;
}C++ Output
Input: 1 1 0 0 0 0 1 0 0 1 1 0 0 1 1 Output: Number of Islands: 3
JAVA Program
public class Main {
static int[][] grid = {
{1,1,0,0,0},
{0,1,0,1,1},
{1,0,0,1,0}
};
static void dfs(int x, int y) {
if(x<0||y<0||x>=3||y>=5||grid[x][y]==0) return;
grid[x][y] = 0;
dfs(x+1,y); dfs(x-1,y); dfs(x,y+1); dfs(x,y-1);
}
public static void main(String[] args) {
int count = 0;
for(int i=0;i<3;i++)
for(int j=0;j<5;j++)
if(grid[i][j]==1) { dfs(i,j); count++; }
System.out.println("Number of Islands: " + count);
}
}JAVA Output
Input: 1 1 0 0 0 0 1 0 1 1 1 0 0 1 0
Output: Number of Islands: 3
Python Program
g = [
[1,1,0,0,0],
[0,1,0,0,0],
[1,0,0,1,1]
]
def dfs(x,y):
if x<0 or y<0 or x>=3 or y>=5 or g[x][y]==0: return
g[x][y]=0
for dx,dy in ((1,0),(-1,0),(0,1),(0,-1)): dfs(x+dx,y+dy)
count = 0
for i in range(3):
for j in range(5):
if g[i][j]==1:
dfs(i,j)
count += 1
print("Number of Islands:", count)Python Output
Input: 1 1 0 0 0 0 1 0 0 0 1 0 0 1 1 Output: Number of Islands: 3
In-Depth Explanation
Example
In the Python grid:
1 1 0 0 0
0 1 0 0 0
1 0 0 1 1
The first island is the cluster of connected 1s at the top-left.
The second island is the 1 at position (2,0).
The third is the two 1s at the bottom-right.
Each set is separated from the others — that's what makes them distinct islands.
Real-Life Analogy
Visualize flying over an ocean and seeing clumps of land.
Every seen set of land, bounded by water and not connected with another set, is counted as a distinct island.
That's precisely what this algorithm does — clustering areas that are internally connected but are not connected to any other areas externally.
Why It Matters
This problem is not merely about land counting; it develops a strong foundation in:
Grid traversal methods
DFS and BFS usage
Connected components in 2D space
It simulates real-world problems such as counting:
Groups of servers
Areas of ground in satellite imagery
Pandemic outbreak areas in pandemic models
Cells in coloring and game grids
Learning Insights
With this problem, you have mastered:
Recursive DFS/BFS in 2D
Preventing revisits using in-place modification
How to model graph search on a matrix
Identifying how islands are like connected components in graph theory
You also understand the relevance of directional movement logic (up, down, left, right) and edge-bound checking.
Interview & Real-World Use
This is one of the most frequently asked DFS/BFS interview questions:
Count islands
Number of provinces
Connected groups
Zombie infection spread
It also lays the foundation for:
Image region segmentation
Cluster analysis
Game map logic (Fog of War, region capture)
Maze and forest exploration problems
Number of islands problem is a building block for graph traversal on grids that aids developers in comprehending depth-first search in the context of space. It's well-known to be applied in both tech interviews and practical scenarios like geographic analysis, map game logic, and computer vision segmentation. With succinct implementations in C, C++, Java, and Python, and solid conceptual description, this tutorial makes you well-prepared to approach 2D DFS questions with confidence.

Social Plugin