📚 PracticeMediumAlgorithm ProblemCoding Ready

Number of Islands

graphdfsbfsmatrix
LeetCode #200
Updated Dec 20, 2025

Question

Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.

LeetCode: Number of Islands

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

Example:

Input: grid = [
  ["1","1","1","1","0"],
  ["1","1","0","1","0"],
  ["1","1","0","0","0"],
  ["0","0","0","0","0"]
]
Output: 1

Input: grid = [
  ["1","1","0","0","0"],
  ["1","1","0","0","0"],
  ["0","0","1","0","0"],
  ["0","0","0","1","1"]
]
Output: 3

Your Solution

python
Auto-saves every 30s

Try solving the problem first before viewing the solution


0:00time spent