class Solution {
public int maxAreaOfIsland(int[][] grid) {
if (grid == null || grid.length < 1 || grid[0].length < 1) return 0;
int m = grid.length, n = grid[0].length;
int res = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
int[] count = new int[1];
dfs(grid, i, j, count);
res = Math.max(res, count[0]);
}
}
}
return res;
}
public void dfs(int[][] grid, int i, int j, int[] count) {
int m = grid.length, n = grid[0].length;
if (i < 0 || i >= m || j < 0 || j >= n || grid[i][j] == 0) return;
grid[i][j] = 0;
count[0]++;
dfs(grid, i + 1, j, count);
dfs(grid, i - 1, j, count);
dfs(grid, i, j - 1, count);
dfs(grid, i, j + 1, count);
}
}