Find the number of islands
Medium Accuracy: 38.66% Submissions: 50184 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.


Your Task:
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)


1 <= n, m <= 500

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Find the number of islands

Output Window