Loading problem...
You are given a two-dimensional binary matrix representing a geographical map where each cell contains either a 0 (representing land) or a 1 (representing water).
A land region is defined as a group of adjacent land cells (value 0) that are connected horizontally or vertically (4-directional connectivity). Two cells are considered part of the same land region if you can travel between them by moving only through adjacent land cells.
An enclosed land region is a land region that is completely surrounded by water on all sides. More specifically, a land region is considered enclosed if and only if:
Your task is to count and return the total number of distinct enclosed land regions in the given grid.
Key Observations:
grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]]2The grid contains two enclosed land regions. The first enclosed region is the connected group of 0s in rows 1-3, columns 1-4 (a roughly rectangular shape). The second enclosed region is the single 0 cell at row 2, column 3 which sits inside the first region but is separated by water. The 0s touching the right edge of the grid are NOT enclosed because they connect to the boundary.
grid = [[0,0,1,0,0],[0,1,0,1,0],[0,1,1,1,0]]1There is exactly one enclosed land region: the single 0 cell at position (1,2) which is completely surrounded by water cells (1s). All other 0 cells in the grid touch the boundary edges, so they cannot form enclosed regions.
grid = [[1,1,1,1,1,1,1],[1,0,0,0,0,0,1],[1,0,1,1,1,0,1],[1,0,1,0,1,0,1],[1,0,1,1,1,0,1],[1,0,0,0,0,0,1],[1,1,1,1,1,1,1]]2This grid features a 'nested' structure. The outer ring of 0s forms one enclosed region. Inside that ring, there is a smaller enclosed region consisting of the single 0 cell at position (3,3). The water cells create concentric barriers that separate these two distinct enclosed land regions.
Constraints