You are given an m x n binary matrix grid, where 0 represents a sea cell and 1 represents a land cell.

A move consists of walking from one land cell to another adjacent (4-directionally) land cell or walking off the boundary of the grid.

Return the number of land cells in grid for which we cannot walk off the boundary of the grid in any number of moves.

&nbsp;
Example 1:

Input: grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
Output: 3
Explanation: There are three 1s that are enclosed by 0s, and one 1 that is not enclosed because its on the boundary.


Example 2:

Input: grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
Output: 0
Explanation: All 1s are either on the boundary or can reach the boundary.


&nbsp;
Constraints:


	m == grid.length
	n == grid[i].length
	1 &lt;= m, n &lt;= 500
	grid[i][j] is either 0 or 1.


class Solution:
    def recursion(self, grid, row, col, m, n):
        if 0<=row<m and 0<=col<n and grid[row][col] == 1:
            grid[row][col] = 't'
            self.recursion(grid, row+1, col, m, n)
            self.recursion(grid, row-1, col, m, n)
            self.recursion(grid, row, col+1, m, n)
            self.recursion(grid, row, col-1, m, n)
    
    def numEnclaves(self, grid: List[List[int]]) -> int:
        m, n = len(grid), len(grid[0])
        if not m or not n:
            return 0
        # mark all boundary lands and neighbors with 0
        for row in range(m):
            self.recursion(grid, row, 0, m, n)
            self.recursion(grid, row, n-1, m, n)
            
        for col in range(n):
            self.recursion(grid, 0, col, m, m)
            self.recursion(grid, m-1, col, m, n)
        
        result = 0
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 1:
                    result += 1
        
        
        return result
    class Solution {
    public int numEnclaves(int[][] grid) {
        int maxcount = 0;
        // if(grid.length==10)
        // {
        // return 3;
        // }
        for(int i = 0;i<grid.length;i++)
          {
              for(int j = 0;j<grid[0].length;j++)
              {
                  if(grid[i][j] ==0)
                  {
                      continue;
                  }
                  if(i==0||i==grid.length-1||j==grid[0].length-1||j==0)
                  {
                        dfs(grid,i,j);
                  }

              }
        }
        int count = 0;
      for(int i = 0;i<grid.length;i++)
          {

              for(int j = 0;j<grid[0].length;j++)
              {

                  if(grid[i][j]==1)
                  {
                      count++;
                  }

              }
        }

        return count;
    }
    int[][] dirs = {{0,1},{1,0},{0,-1},{-1,0}};

      public void dfs(int[][] grid,int row,int col) {

           grid[row][col] = 0;
             for(int k = 0;k<4;k++)
         {
             int rowdash = row+dirs[k][0];
             int coldash = col+dirs[k][1];

             if(rowdash<0||coldash<0||rowdash>=grid.length||coldash>=grid[0].length||
              grid[rowdash][coldash]==0)
             {
                 continue;
             }

             if(grid[rowdash][coldash]==1)
             {
               dfs(grid,rowdash,coldash);
             }

         }

    }
}
class Solution {
public:
    int numEnclaves(vector<vector<int>>& grid) {
        for(int i=0;i<grid.size();i++)
        {
            for(int j=0;j<grid[0].size();j++)
            {
                if(grid[i][j]==0)
                    continue;
                int c=0;
                if(grid[i][j]==1 && (i*j==0 || i==grid.size()-1 || j==grid[0].size()-1))
                {
                    queue<vector<int>> q;
                    q.push({i,j});
                    while(!q.empty())
                    {
                        vector<int> a;
                        a=q.front();
                        q.pop();
                        grid[a[0]][a[1]]=0;
                        if(a[0]+1<grid.size() && grid[a[0]+1][a[1]]==1)
                        {
                            q.push({a[0]+1,a[1]});
                            grid[a[0]+1][a[1]]=0;
                        }
                        if(a[1]+1<grid[0].size() && grid[a[0]][a[1]+1]==1)
                        {
                            q.push({a[0],a[1]+1});
                            grid[a[0]][a[1]+1]=0;
                        }
                        if(a[0]-1>=0 && grid[a[0]-1][a[1]]==1)
                        {
                            q.push({a[0]-1,a[1]});
                            grid[a[0]-1][a[1]]=0;
                        }
                        if(a[1]-1>=0 && grid[a[0]][a[1]-1]==1)
                        {
                            q.push({a[0],a[1]-1});
                            grid[a[0]][a[1]-1]=0;
                        }
                    }
                }
            }
        }
        int m=0;
        for(int i=0;i<grid.size();i++)
        {
            for(int j=0;j<grid[0].size();j++)
            {
                m+=grid[i][j];
            }
        }
        return m;
    }
};
/**
 * @param {number[][]} grid
 * @return {number}
 */
var numEnclaves = function(grid) {

    let count = 0, rowLength = grid.length, colLength = grid[0].length

    const updateBoundaryLand = (row,col) => {
        if(grid?.[row]?.[col]){
            grid[row][col] = 0
            updateBoundaryLand(row + 1,col)
            updateBoundaryLand(row - 1,col)
            updateBoundaryLand(row,col + 1)
            updateBoundaryLand(row,col - 1)

        }
    }

    for(let i=0;i<rowLength;i++){
        if(grid[i][0] || grid[i][colLength-1]){
            updateBoundaryLand(i,0)
            updateBoundaryLand(i,colLength-1)
        }
    }

    for(let j=0;j<colLength;j++){
        if(grid[0][j] || grid[rowLength-1][j]){
            updateBoundaryLand(0,j)
            updateBoundaryLand(rowLength-1,j)
        }
    }

    for(let i=0;i<rowLength;i++){
        for(let j=0;j<colLength;j++){
            if(grid[i][j]) count++
        }
    }

    return count

};
Number of Enclaves