Max Area of Island
Published at25 Jul 2026
Created by M Ichsanul Fadhil
You are given a 2D grid containing '1' (land) and '0' (water). Find the maximum area of an island. An island is formed by connecting adjacent land cells horizontally or vertically.
Use DFS to explore each island. When a '1' is found, compute its area via DFS by counting all connected land cells, then mark them as visited. Track the maximum area found.
Time is O(m*n) and space is O(m*n) for the recursion stack.
Example Input & Output
Example 1
Input
[["0"]]
Output
0
Explanation
No land
Example 2
Input
[["1"]]
Output
1
Explanation
Single cell
Example 3
Input
[["1","1"],["1","1"]]
Output
4
Explanation
2x2 island
Example 4
Input
[["0","0","0"],["0","0","0"]]
Output
0
Explanation
No land
Example 5
Input
[["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"]]
Output
6
Explanation
Max area is 6 (the island in top-right)
Algorithm Flow

Recommendation Algorithm Flow for Max Area of Island
Solution Approach
Best Answers
java
class Solution {
int r,c;
public int solution(String[][] g) {
if(g==null||g.length==0)return 0;r=g.length;c=g[0].length;int mx=0;
for(int i=0;i<r;i++)for(int j=0;j<c;j++)if(g[i][j].equals("1"))mx=Math.max(mx,dfs(g,i,j));
return mx;
}
int dfs(String[][] g,int i,int j){
if(i<0||i>=r||j<0||j>=c||g[i][j].equals("0"))return 0;
g[i][j]="0";return 1+dfs(g,i-1,j)+dfs(g,i+1,j)+dfs(g,i,j-1)+dfs(g,i,j+1);
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
