leetcode695. 岛屿的最大面积

class Solution {
public:
    int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
    int dfs(int x,int y,vector<vector<int>>& grid){
        int m=grid.size(),n=grid[0].size();
        if(x<0||x>=m||y<0||y>=n||!grid[x][y])return 0;
        int ans=1;
        grid[x][y]=0;
        for(int i=0;i<4;i++){
            int xx=x+dx[i],yy=y+dy[i];
            ans+=dfs(xx,yy,grid);
        }
        return ans;
    }
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int m=grid.size(),n=grid[0].size();
        int ans=0;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(grid[i][j]){
                    int t=dfs(i,j,grid);
                    ans=max(ans,t);
                }
            }
        }
        return ans;
    }
};