import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param grid int整型二维数组 
     * @return int整型
     */
    public int maxAreaIsland (int[][] grid) {
        // write code here
        
        int n = grid.length;
        int m = grid[0].length;
        
        int res = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] == 1) {
                    res = Math.max(res, process(grid, i, j, 0, n, m));
                }
            }
        }
        return res;
    }
    
    public int process(int[][] grid, int i, int j, int area, int n, int m) {
        if (i < 0 || i >= n || j < 0 || j >= m || grid[i][j] == 0) {
            return area;
        }
        grid[i][j] = 0;
        int u = process(grid, i - 1, j, area, n, m);
        int d = process(grid, i + 1, j, area, n, m);
        int l = process(grid, i, j - 1, area, n, m);
        int r = process(grid, i, j + 1, area, n, m);
        return u + d + l + r + 1;
    }
}