class Solution {
public:
int result = 0 , tmp_size = 1;
int m =0 ,n=0;
bool borad_flag = false;
int dir[4][2] = {0,1, 0,-1 , -1,0 , 1,0};
void dfs(vector<vector<int>>& grid , vector<vector<bool>> &path , int x , int y)
{
for(int i=0 ; i<4 ;i++)
{
int next_x = x + dir[i][0];
int next_y = y + dir[i][1];
if(next_x<0||next_x>=m||next_y<0||next_y>=n)
{
borad_flag = true;
continue;
}
if( path[next_x][next_y] == false && grid[next_x][next_y] == 1)
{
tmp_size++;
path[next_x][next_y] = true;
dfs(grid,path,next_x,next_y);
}
}
return;
}
int numEnclaves(vector<vector<int>>& grid) {
m = grid.size();
n = grid[0].size();
vector<vector<bool>> path( m , vector<bool>( n ,false) );
for(int i=0 ; i<m ;i++)
{
for(int j=0 ; j<n ;j++)
{
if(path[i][j] == false && grid[i][j] == 1)
{
tmp_size = 1;
borad_flag = false;
path[i][j] = true;
dfs(grid,path,i,j);
if(borad_flag == false ) result += tmp_size;
}
}
}
return result;
}
};