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:


Input:
11110
11010
11000
00000

Output: 1


Example 2:


Input:
11000
11000
00100
00011

Output: 3

Source: leetcode | Difficulty: MEDIUM_HARD
Tags: Matrices

Data structures

Graph traversal

Spanning trees

Starter code: class Solution:
    def numIslands(self, grid: List[List[str]]) -> int:
        

Solutions:
class Solution:

	def expandIsland(self, grid, i, j):
		edges = [(i, j)]
		while edges:
			next_edges = []
			for edge in edges:
				(ei, ej) = edge
				if ei >= 0 and ei < len(grid) and (ej >= 0) and (ej < len(grid[ei])) and (grid[ei][ej] == '1'):
					grid[ei][ej] = '2'
					next_edges.append((ei + 1, ej))
					next_edges.append((ei, ej + 1))
					next_edges.append((ei - 1, ej))
					next_edges.append((ei, ej - 1))
			edges = next_edges

	def numIslands(self, grid):
		island_count = 0
		for i in range(len(grid)):
			for j in range(len(grid[i])):
				if grid[i][j] == '1':
					island_count += 1
					self.expandIsland(grid, i, j)
		return island_count