This question evaluates understanding of graph traversal and connected-components detection on a 2D grid, measuring the ability to model grid cells as nodes with four-directional adjacency and reason about connectivity.
You are given an m x n grid of characters '0' and '1':
'1'
represents land
'0'
represents water
Two land cells are connected if they share an edge (4-directional adjacency: up/down/left/right).
Task: Return the number of connected land components ("islands").
grid
:
m x n
matrix of characters
'0'
/
'1'
1 <= m, n <= 300
grid[i][j] ∈ {'0','1'}
Input:
1 1 0 0
1 0 0 1
0 0 1 1
Output:
3