Problem
You get a map grid with m rows and n columns. Each cell is the string "1" for land or the string "0" for water.
An island is a group of land cells that connect horizontally or vertically. Two cells that touch only at a corner do not connect. All the cells outside the grid are water.
Return the number of islands in grid.
Example 1
- Input:
- grid = [["1","1","0","0"],["0","1","0","1"],["0","0","0","1"],["1","0","0","0"]]
- Output:
- 3
- Explanation:
- One island has 3 cells in the top-left corner. One island has 2 cells at the right edge. The cell in the bottom-left corner is the third island.
Example 2
- Input:
- grid = [["1","0","1"],["0","1","0"],["1","0","1"]]
- Output:
- 5
- Explanation:
- The land cells touch only at their corners. Each land cell is a separate island.
Example 3
- Input:
- grid = [["0","0"],["0","0"]]
- Output:
- 0
Constraints
- m == grid.length
- n == grid[i].length
- 1 <= m, n <= 300
- grid[i][j] is "0" or "1"
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Number of Distinct Islands
Coding · Array · Breadth-First Search · Depth-First Search
- Making A Large Island
Coding · Array · Breadth-First Search · Depth-First Search
- Maximum Number of Points From Grid Queries
Coding · Array · Breadth-First Search · Heap (Priority Queue)
- Accounts Merge
Coding · Array · Breadth-First Search · Depth-First Search
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 13 records
Amazon
Leetcode 200. Number of Islands
Reported Jun 24, 2026 · 3 source reports
MID_LEVEL, SENIOR
Source snapshot: Nov 12, 2026
View sourceApple
Leetcode 200. Number of Islands
Reported Sep 25, 2026 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View sourceDocusign
Leetcode 200. Number of Islands
Reported Sep 29, 2026 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View sourceLinkedIn
Leetcode 200. Number of Islands
Reported Sep 1, 2026 · 9 source reports
MID_LEVEL, SENIOR
Source snapshot: Nov 12, 2026
View source