Given a grid of land and water, return the number of land cells that cannot reach the boundary by moving only in four directions. Pattern focus: Component traversal. Traverse each land component and ignore the ones that touch the border.
grid = 2D binary matrix
count of enclave land cells
Example 1:
Input:
grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
Output:
3
Explanation:
Three land cells are fully enclosed away from the boundary.
Example 2:
Input:
grid = [[1,1,1],[1,0,1],[1,1,1]]
Output:
0
Explanation:
Every land cell touches the border through the component.
Example 3:
Input:
grid = [[0,0],[0,0]]
Output:
0
Explanation:
There is no land.