```
class Solution(object):
def numIslands(self, grid):
dir = [[0, 1], [0, -1], [1, 0], [-1, 0]]
def dfs(i, j):
if not( 0<=i<len(grid) and 0<=j<len(grid[0]) ) or grid[i][j] == '0':
return
grid[i][j] = '0'
for l in xrange(len(dir)):
dfs( i + dir[l][0], j + dir[l][1] )
if not grid:
return 0
ans = 0
for i in xrange(len(grid)):
for j in xrange(len(grid[0])):
if grid[i][j] == '1':
ans += 1
dfs(i, j)
return ans
```