class Solution { public int numEnclaves(int[][] grid) { int n = grid.length; int m = grid[0].length; int[][] visited = new int[n][m]; // step-1 check the boundary characters // traversing through boundary 0th col, last row, last col, 0th row for(int i=0; i= 0 && nCol >= 0 && nRow < n && nCol < m) { // checking if neighbour cell is land cell if(visited[nRow][nCol] == 0 && grid[nRow][nCol] == 1) { dfs(grid, visited, nRow, nCol, n, m); } } } } }