Dsa
Matrix
Easy
Count Islands in a Matrix
Given a 2D matrix filled with 0's and 1's, count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. The input is given as:
- The first line contains two integers n and m (1 ≤ n, m ≤ 100).
- The next n lines contain m integers (0 or 1) representing the matrix.
Output the number of islands as a single integer.
Example:
Input:
4 5
1 1 0 0 0
1 0 0 1 0
0 0 0 1 1
0 0 0 0 0
Output:
3
Key concepts
matrixislandsearchbfs
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