← Back to all problems
Medium

Number of Islands

Updated Feb 24, 2026

Problem

Count connected components of '1' in a grid.

Constraints

1 <= m, n <= 300

Examples

Example 1

Input: {"grid": [["1", "1", "1", "1", "0"], ["1", "1", "0", "1", "0"], ["1", "1", "0", "0", "0"], ["0", "0", "0", "0", "0"]]}
Output: 1

Function Signature

def numIslands(self, grid: list[list[str]]) -> int

How to Submit

Implement a Solution class with a numIslands method.

Your method will be called with the input parameters and should return the answer.