This question evaluates grid traversal and connected-component detection skills, focusing on 4-directional adjacency and boundary-condition reasoning in matrix-based maps.
You are given an m x n 2D grid representing a map after flooding:
0
= water
1
= land
An island is a maximal 4-directionally connected component of land cells (1s), where connectivity is via up, down, left, right (not diagonals).
A closed island is an island that does not touch the grid boundary (i.e., none of its cells lie in the first/last row or first/last column). Any island that touches the boundary is not counted.
Return the number of closed islands in the grid.
grid
:
int[m][n]
int
: the number of closed islands
m, n >= 1
.
If an island is entirely surrounded by water but a land cell touches the border, it should not be counted as closed.