#### QUESTION:

[[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]] 对于上面这个给定矩阵应返回 6。注意答案不应该是11，因为岛屿只能包含水平或垂直的四个方向的‘1’。

[[0,0,0,0,0,0,0,0]] 对于上面这个给定的矩阵, 返回 0。

#### EXPLANATION:

1.创建一个二维的boolean类型数组，用以标记已经计算了的1，并且放置首尾循环
2.遍历数组，遇到1并且没有进行标记，那么就开始计算这个[i,j]对应的值，也就是他的前后左右的结果
3.如果前后左右同样还有1，那么就可以进行递归遍历，重复2-3步骤
4.[i,j]处的结果就是 left+right+top+bottom的结果
5.利用了boolean的二维数组，可以有效的减少重复计算的问题

#### SOLUTION:

``````class Solution {
public int maxAreaOfIsland(int[][] grid) {
boolean[][] alreadyCount = new boolean[grid.length][grid[0].length];
int result  = 0;
for(int i = 0;i<grid.length;i++){
for(int j = 0;j<grid[i].length;j++){
if(grid[i][j]==1 && !alreadyCount[i][j]) result = Math.max(result,maxAreaOfIslandHelper(grid,alreadyCount,i,j));
}
}
return result;
}

public static int maxAreaOfIslandHelper(int[][] grid,boolean[][] alreadyCount,int i,int j){
if(i >=grid.length || i<0) return 0;
if(j>=grid[0].length||j<0) return 0;
if(alreadyCount[i][j] || grid[i][j]==0) return 0;
alreadyCount[i][j] = true;
int left = maxAreaOfIslandHelper(grid,alreadyCount,i,j-1);
int right = maxAreaOfIslandHelper(grid,alreadyCount,i,j+1);
int top = maxAreaOfIslandHelper(grid,alreadyCount,i-1,j);
int bottom = maxAreaOfIslandHelper(grid,alreadyCount,i+1,j);
return left+right+top+bottom+1;
}
}
``````
>