# My DFS solution

• ``````class Solution {

public int area, maxArea = Integer.MIN_VALUE;

public int maxAreaOfIsland(int[][] grid) {
int i, j, m = grid.length, n = grid[0].length;
boolean[][] visited = new boolean[m][n];

for(i = 0; i < m; i ++) {
for(j = 0; j < n; j ++) {
visited[i][j] = false;
}
}
for(i = 0; i < m; i ++) {
for(j = 0; j < n; j ++) {
area = 0;
if(grid[i][j] == 1 && visited[i][j] == false) {
dfs(i, j, grid, visited);
}
if(area > maxArea) maxArea = area;
}
}
return maxArea;
}

public void dfs(int i, int j, int[][] grid, boolean[][] visited) {
int m = grid.length, n = grid[0].length;
if(visited[i][j] == false) {
visited[i][j] = true;
area ++;
if(j + 1 < n && visited[i][j + 1] == false && grid[i][j + 1] == 1) dfs(i, j + 1, grid, visited);
if(j - 1 >= 0 && visited[i][j - 1] == false && grid[i][j - 1] == 1) dfs(i, j - 1, grid, visited);
if(i - 1 >= 0 && visited[i - 1][j] == false && grid[i - 1][j] == 1) dfs(i - 1, j, grid, visited);
if(i + 1 < m && visited[i + 1][j] == false && grid[i + 1][j] == 1) dfs(i + 1, j, grid, visited);
return;
}
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.