Problem
You are given an m x n 2D grid representing a map after flooding:
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.
Task
Return the number of closed islands in the grid.
Input
Output
-
int
: the number of closed islands
Notes / Assumptions
-
Use 4-directional adjacency.
-
You may assume
m, n >= 1
.
Example
If an island is entirely surrounded by water but a land cell touches the border, it should not be counted as closed.