X

DAYS

:

HOUR

:

MINS

:

SEC

Copied to Clipboard
Find the number of islands
Medium Accuracy: 38.66% Submissions: 93596 Points: 4

Given a grid consisting of '0's(Water) and '1's(Land). Find the number of islands.
Note: An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically or diagonally i.e., in all 8 directions.

Example 1:

Input:
grid = {{0,1},{1,0},{1,1},{1,0}}
Output:
1
Explanation:
The grid is-
0 1
1 0
1 1
1 0
All lands are connected.


Example 2:

Input:
grid = {{0,1,1,1,0,0,0},{0,0,1,1,0,1,0}}
Output:
2
Expanation:
The grid is-
0 1 1 1 0 0 0
0 0 1 1 0 1 0
There are two islands one is colored in blue
and other in orange.


You don't need to read or print anything. Your task is to complete the function numIslands() which takes grid as input parameter and returns the total number of islands.

Expected Time Compelxity: O(n*m)
Expected Space Compelxity: O(n*m)

Constraints:
1 ≤ n, m ≤ 500

We are replacing the old Disqus forum with the new Discussions section given below.