We may use an extra visited[][] so not to modify the input grid.

class Solution { private int I, J; private int[][] g; private boolean[][] visited; private int dfs(int i, int j) { if (i < 0 || i >= I || j < 0 || j >= J) return 0; if (visited[i][j]) return 0; visited[i][j] = true; if (g[i][j] == 0) return 0; return dfs(i - 1, j) + dfs(i + 1, j) + dfs(i, j - 1) + dfs(i, j + 1) + 1; } public int maxAreaOfIsland(int[][] grid) { g = grid; I = g.length; J = g[0].length; visited = new boolean[I][J]; int maxArea = 0; for (int i = 0; i < I; i++) for (int j = 0; j < J; j++) if (!visited[i][j] && g[i][j] == 1) maxArea = Math.max(maxArea, dfs(i, j)); return maxArea; } }Max Area of Island