Number of Islands
A rectangular grid uses integer 1 for land and 0 for water. Two land cells are adjacent when they share an edge or a corner: movement uses all eight…
A rectangular grid uses integer 1 for land and 0 for water. Two land cells are adjacent when they share an edge or a corner: movement uses all eight neighboring directions. An island is one maximal connected group of land cells. Return the number of islands without changing the grid. Count groups, not individual cells.
- 1 <= rows, columns <= 300 - Every row has the same length; every value is integer 0 or 1 - Diagonal contact connects land - Preset visualization accepts up to 6×6; larger valid grids are execution-only
Example 1: input grid = [[1,0,0],[0,1,0],[0,0,1]]; output 1. Each consecutive land cell touches the next at a corner, joining all three into one island.
Example 2: input grid = [[1,0,1],[1,0,1]]; output 2. The water column separates the two land groups; neither edge nor corner contact crosses it.
Category: Graphs. Topic: graph. Difficulty: Medium. Patterns: graph-traversal.