# leetcode_695_岛屿的最大面积

```[[0,0,1,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,1,1,0,1,0,0,0,0,0,0,0,0],
[0,1,0,0,1,1,0,0,1,0,1,0,0],
[0,1,0,0,1,1,0,0,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,0,0,0,0,0,0,1,1,0,0,0,0]]
```

`[[0,0,0,0,0,0,0,0]]`

``````class Solution {
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
int area=0;
int temp=0;
for(int i=0;i<grid.size();i++){
for(int j=0;j<grid[0].size();j++){
if(grid[i][j]==1){
temp=calcMax(grid,i,j);
area=max(area,temp);
}
}
}
return area;
}

int calcMax(vector<vector<int>>& grid,int i,int j){
int n = 1;
grid[i][j] = 0;
if(i+1<grid.size() && grid[i+1][j] == 1){
n += calcMax(grid, i+1, j);
}
if(j+1<grid[0].size() && grid[i][j+1] == 1){
n += calcMax(grid, i, j+1);
}
if(i-1>=0 && grid[i-1][j] == 1){
n += calcMax(grid, i-1, j);
}
if(j-1>=0 && grid[i][j-1] == 1){
n += calcMax(grid, i, j-1);
}
return n;
}

};``````

``````class Solution {
public:
int maxAreaOfIsland(vector<vector<int>>& grid) {
int m=grid.size(), n=grid[0].size();
int max_a=0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j]==1){
int a = islandDFS(grid, m, n, i, j);
max_a = max(max_a, a);
}
}
}
return max_a;
}

int islandDFS(vector<vector<int>>& grid, int& m, int& n, int i, int j){
if (i<0 || i>=m || j<0 || j>=n || grid[i][j]==0)
return 0;
else{
grid[i][j] = 0;
return 1+islandDFS(grid, m, n, i+1, j)+islandDFS(grid, m, n, i-1, j)+
islandDFS(grid, m, n, i, j+1)+islandDFS(grid, m, n, i, j-1);
}
}
};``````

;