Number of Enclaves

Given an N × M binary grid, 0 denotes sea and 1 denotes land. From a land cell you may move up, right, down or left to another land cell, or walk off the…

Given an N × M binary grid, 0 denotes sea and 1 denotes land. From a land cell you may move up, right, down or left to another land cell, or walk off the boundary. Return the number of land cells from which no sequence of these moves can leave the grid. Count cells, not connected components.

- 1 <= N, M <= 500 - grid is rectangular and every cell is integer 0 or 1 - The preset visualizer accepts grids up to 8×8; larger valid grids are execution-only.

Example 1: input grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]; output 3. The three interior connected land cells have no orthogonal land path to the boundary; boundary-connected land does not count.

Example 2: input grid = [[0,0,0,1],[0,0,0,1],[0,1,1,0],[0,0,1,0],[0,0,0,0]]; output 3. The three interior connected land cells have no orthogonal land path to the boundary; boundary-connected land does not count.

Category: Graphs. Topic: graph. Difficulty: Medium. Patterns: graph-traversal.