```
class Solution(object):
def clear(self,grid,r,c) :
grid[r][c] = 0
if r > 0 and grid[r-1][c] =='1': self.clear(grid,r-1,c)
if r+1 < len(grid) and grid[r+1][c] == '1' : self.clear(grid, r+1,c)
if c > 0 and grid[r][c-1] == '1' : self.clear(grid,r,c-1)
if c+1 < len(grid[0]) and grid[r][c+1] == '1' : self.clear(grid,r,c+1)
def numIslands(self, grid):
"""
:type grid: List[List[str]]
:rtype: int
"""
if not grid :
return 0
rows = len(grid)
cols = len(grid[0])
res = 0
for r in range(rows) :
for c in range(cols) :
if grid[r][c] == '1':
res +=1
self.clear(grid,r,c)
return res
```