| Topic | Difficulty | Companies |
|---|---|---|
| Graph | MEDIUM | Microsoft Amazon LinkedIn |
Given a 2D grid map of '1' s (land) and '0' s (water), write a program to count the number of islands.
Problem Note


Example 1
Input: grid[][] = [
[1, 1, 0, 0, 0],
[0, 1, 0, 0, 1],
[0, 0, 0, 1, 1],
[0, 0, 0, 0, 0],
[1, 1, 1, 0, 1]
]
Output: 4
Example 2
Input: grid[][] = [
[1, 1, 0, 0, 0],
[0, 0, 1, 0, 1],
[0, 0, 0, 1, 1],
[0, 0, 0, 0, 1],
[1, 1, 1, 0, 1]
]
Output: 2