Imagine exploring a vast archipelago where islands are scattered across the ocean! You're given a 2D grid representing this maritime landscape, where 0 represents land and 1 represents water.
An island is a group of connected land cells (0s) that are adjacent horizontally or vertically. But here's the twist: we're only interested in closed islands - islands that are completely surrounded by water on all sides, including the boundaries of the grid.
Goal: Count how many closed islands exist in this grid. An island touching the edge of the grid doesn't count as closed since it's not completely surrounded by water!
Example: In a grid where land extends to the border, those border islands are not closed because they're not fully encircled by water.
Input & Output
Constraints
- 1 β€ grid.length, grid[i].length β€ 100
- grid[i][j] is 0 (land) or 1 (water)
- Islands are formed by connecting adjacent land cells horizontally or vertically