class Solution {
public int numIslands(char[][] grid) {
int m= grid.length;
if(m==0) return 0;
int n= grid[0].length;
int count=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(grid[i][j]=='1')
{
count++;
grid[i][j]='2';
dfs(grid,i,j);
}
}
}
return count;
}
public void dfs(char [][] grid,int i,int j)
{
if(i+1<grid.length&&grid[i+1][j]=='1')
{
grid[i+1][j]='2';
dfs(grid,i+1,j);
}
if(i-1>=0&&grid[i-1][j]=='1')
{
grid[i-1][j]='2';
dfs(grid,i-1,j);
}
if(j+1<grid[0].length&&grid[i][j+1]=='1')
{
grid[i][j+1]='2';
dfs(grid,i,j+1);
}
if(j-1>=0&&grid[i][j-1]=='1')
{
grid[i][j-1]='2';
dfs(grid,i,j-1);
}
}
}