You're exploring a mysterious archipelago represented by a binary matrix where 0 represents water and 1 represents land. Your goal is to find landlocked islands - pieces of land that are completely surrounded by water or other land, with no path to escape to the ocean.
Think of it this way: if you were standing on a land cell, you can move in 4 directions (up, down, left, right) to adjacent land cells. If there's any possible path that leads you to the boundary of the grid (the edge where you can "walk off" into the ocean), then that land is not an enclave.
Return the total number of land cells that form enclaves - land from which you cannot reach the boundary no matter how many moves you make.
Example: In a 4ร4 grid, if there's a small island in the center completely surrounded by water or boundary-connected land, those center cells would be enclaves.
Input & Output
Constraints
- m == grid.length
- n == grid[i].length
- 1 โค m, n โค 500
- grid[i][j] is either 0 or 1