human
I am working on the file "04_BFS.ipynb".
The first task is:
BFS in Matrix

Number of Islands

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
11110
11010
11000
00000
Answer: 1
Example 2:
11000
11000
00100
00011
Answer: 3
https://leetcode.com/problems/number-of-islands/#/description
http://www.lintcode.com/en/problem/number-of-islands/
Can you write Python code for it?

=====
Can you provide a the Python code you requested?
assistant
#Here is the Python code you requested.

class Solution(object):
    def numIslands(self, grid):
        """
        :type grid: List[List[str]]
        :rtype: int
        """
        if grid is None or len(grid) == 0 or len(grid[0]) == 0:
            return 0
        
        m, n = len(grid), len(grid[0])
        visited = [[False for i in range(n)] for j in range(m)]
        count = 0
        
        def bfs(grid, m, n, x, y):
            dx = [0, 0, 1, -1]
            dy = [1, -1, 0, 0]
            
            queue = [[x, y]]
            visited[x][y] = True
            while queue:
                x, y = queue.pop(0)
                for i in range(4):
                    xx, yy = x + dx[i], y + dy[i]
                    if 0 <= xx < m and 0 <= yy < n:
                        if not visited[xx][yy] and grid[xx][yy] == '1':
                            queue.append([xx, yy])
                        visited[xx][yy] = True
        
        for x in range(m):
            for y in range(n):
                if not visited[x][y] and grid[x][y] == '1':
                    count += 1
                    bfs(grid, m, n, x, y)

        
        return count
        

class Solution:
    # @param {boolean[][]} grid a boolean 2D matrix
    # @return {int} an integer
    def numIslands(self, grid):
        # Write your code here
        if grid is None or len(grid) == 0 or len(grid[0]) == 0:
            return 0
            
        m, n = len(grid), len(grid[0])
        visited = [[False for y in range(n)] for x in range(m)]
        dx = [0, 0, 1, -1]
        dy = [1, -1, 0, 0]
        count = 0
        
        def bfs(x, y):
            queue = [[x, y]]
            while queue:
                x, y = queue.pop(0)
                visited[x][y] = True
                for i in range(4):
                    xx, yy = x + dx[i], y + dy[i]
                    if 0 <= xx < m and 0 <= yy < n:
                        if not visited[xx][yy] and grid[xx][yy] == 1:
                            queue.append([xx, yy])
                        visited[xx][yy] = True
    ...