Dsa
Graphs Bfs
Medium
Connected Components in a Grid
You are given a 2D grid of '0's (water) and '1's (land). A connected component is defined as a group of adjacent '1's (horizontally or vertically). Write a program that determines the number of connected components of '1's in the grid. Input consists of the integer N (the number of rows) followed by N lines of '0's and '1's. Output the count of connected components.
Example:
Input:
4
1100
1100
0010
0001
Output:
2
Key concepts
graphsbfsgrid
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