← Problems

Number of Islands

MEDIUM
Problem⌘ K
Graph BFSMatrix

Number of Islands

Given a 2D grid of '1' land and '0' water, count the number of islands. An island is connected horizontally or vertically, not diagonally.

Example

Input grid = [[1,1,0],[0,1,0],[0,0,1]]
Output 2

Constraints

m and n are between 1 and 300.grid[i][j] is either '0' or '1'.

Think about

Try a first approach in the editor. Copilot reviews your actual code and asks one focused question when it finds a gap.

solution.c
Test resultsReady
$ Type your approach, then run the sample tests.
LIVE AI REVIEW · WAITING
Start typing or paste your attempt here.