class Solution {
public:
int result = 0;
int m,n;
int dir[4][2] = {0,-1,0,1,-1,0,1,0};
void dfs(vector<vector<int>>& grid ,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) result++;
else if(grid[next_x][next_y] == 0) result++;
}
return;
}
int islandPerimeter(vector<vector<int>>& grid) {
m = grid.size();
n = grid[0].size();
for(int i=0 ; i<m ;i++)
{
for(int j=0 ; j<n ;j++)
{
if(grid[i][j] == 1)
dfs(grid,i,j);
}
}
return result;
}
};