# -*- coding:utf-8 -*-
class Solution(object):
    def __init__(self):
        self.dx, self.dy = [-1, 1, 0, 0], [0, 0, -1, 1]

    def dfs_fill(self, grid, i, j, m, n):
        if grid[i][j] == '0':
            return 0
        grid[i][j] = '0'
        for k in range(len(self.dx)):
            x, y = i + self.dx[k], j + self.dy[k]
            if not (x < 0 or y < 0 or x >= m or y >= n):
                self.dfs_fill(grid, x, y, m, n)
        return 1

    def numIslands(self, grid):
        """
        :type grid: List[List[str]]
        :rtype: int
        """
        lands = 0
        if not grid:
            return lands
        m, n = len(grid), len(grid[0])
        for i in range(m):
            for j in range(n):
                if grid[i][j] == '1':
                    lands += self.dfs_fill(grid, i, j, m, n)
        return lands
