Skip to content
BytePatterns

Count Island Blobs

MediumGraphs#dfs#flood-fill#grid-traversal~30m

Problem

A rectangular grid holds 1 for land and 0 for water. An island is a group of land cells connected to each other through shared edges, so cells touching only at a corner belong to different islands. Count how many separate islands the grid contains.

Examples

Input:  [[1, 1, 0],
         [0, 1, 0],
         [0, 0, 1]]
Output: 2
Why:    the corner-touching cell at the bottom right is its own island
Input:  [[0, 0],
         [0, 0]]
Output: 0
Why:    edge case, a grid of pure water has no islands
Input:  [[1]]
Output: 1
Why:    edge case, a single land cell is a complete island

Hints

0 / 3

Stuck on the idea rather than the code? Connected Components covers it.