Dsa
Graphs Dfs
Easy
Safe Zones in Flooded Areas
You are given a grid representing areas affected by flooding where 0 represents safe areas and 1 represents flooded areas. Write a program that counts the number of distinct safe zones (connected components of 0's) in the grid. A safe zone is connected vertically or horizontally.
Input format:
- The first line contains two integers, rows and cols (1 ≤ rows, cols ≤ 100).
- The next rows lines each contain cols integers (0 or 1) representing the grid.
Output format:
- A single integer indicating the number of distinct safe zones.
Example:
Input:
4 5
0 1 0 0 1
0 0 0 1 1
1 1 0 0 0
1 0 0 1 0
Output:
3
Key concepts
graphsdfsflood fill
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