1020. Number of Enclaves
T: O(mn)
S: O(1)
```java
class Solution {
private int m;
private int n;
private static final int[][] DIRECTIONS = {{0,1},{1,0}, {0,-1},{-1,0}};
public int numEnclaves(int[][] grid) {
m = grid.length;
n = grid[0].length;
// exclude edge connected land
for (int i = 0; i < m ; i++) {
for (int j = 0; j < n; j++) {
if ((i == 0 || i == m-1 || j == 0 || j == n-1) && grid[i][j] == 1) {
dfs(grid, i, j);
}
}
}
// only need to count left 1 (moves)
int count = 0;
for (int i = 0; i < m ; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
count++;
}
}
}
return count;
}
// flood fill dfs
private void dfs(int[][] grid, int i, int j) {
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] != 1) {
return;
}
grid[i][j] = 0;
for (int[] direction : DIRECTIONS) {
dfs(grid, i + direction[0], j + direction[1]);
}
}
}
```
Last updated