992. Count Islands Using BFS

EasyGraphsGridBFSGraph

Given a 2D grid of 1s (land) and 0s (water), count the number of islands using a BFS-based flood fill. An island is a maximal group of 1s connected horizontally or vertically. The input is JSON {grid}.

Input: JSON {grid}.

Output: Integer — the number of islands.

Examples

Example 1
Input: {"grid":[[1,1,0,0],[1,0,0,1],[0,0,1,1]]}
Output: 2
Explanation: Two land masses.
Example 2
Input: {"grid":[[0]]}
Output: 0
Explanation: All water.
Example 3
Input: {"grid":[[1,0,1]]}
Output: 2
Explanation: Two separate cells.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →