Dsa
Graphs Dfs
Easy
Connected Components Count
Given a 2D grid of '1's (land) and '0's (water), write a program to find the number of connected components in the grid. A connected component is a maximal group of '1's connected orthogonally (up, down, left, right).
Input format:
- The first line contains two integers, m and n, the dimensions of the grid.
- The following m lines each contain n characters ('0' or '1').
Output format:
- An integer representing the number of connected components of '1's.
Example:
Input:
4 4
1100
1100
0011
0000
Output:
2
Key concepts
graphsdfscomponents
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now