Number of Islands

Medium Solved

Description

Given an m × n grid filled with '1' (land) and '0' (water), return the number of islands.

An island is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are surrounded by water.

Input format:

Runner expects a single line stdin containing a JSON 2D array.

Example:

Input:
[["1","1","0","0"],
 ["1","1","0","0"],
 ["0","0","1","0"],
 ["0","0","0","1"]]

Output:
3

Note:

Print only a single integer representing the number of islands.

No submissions yet.

Discuss DFS, BFS, visited marking, and grid traversal approaches.

Test Cases