```
public class Solution {
public int NumIslands(char[,] grid) {
int m=grid.GetLength(0), n = grid.GetLength(1);
var visited = new bool[m,n];
var count = 0;
for(var i=0;i<m;i++){
for(var j=0;j<n;j++){
if(visited[i,j]||grid[i,j]=='0') continue;
count++;
dfs(grid,i,j, m , n, visited);
}
}
return count;
}
public void dfs(char[,] grid, int x, int y, int m, int n, bool[,] visited){
if(visited[x,y]) return;
visited[x,y] = true;
if(x>0 && grid[x-1,y] =='1') dfs(grid, x-1, y, m, n, visited);
if(y>0 && grid[x,y-1] =='1') dfs(grid, x, y-1, m, n, visited);
if(x<m-1 && grid[x+1,y] =='1') dfs(grid, x+1, y, m, n, visited);
if(y<n-1 && grid[x,y+1] =='1') dfs(grid, x, y+1, m, n, visited);
}
}
```