<p>Given an <code>m x n</code> 2D binary grid <code>grid</code> which represents a map of <code>&#39;1&#39;</code>s (land) and <code>&#39;0&#39;</code>s (water), return <em>the number of islands</em>.</p>

<p>An <strong>island</strong> 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.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> grid = [
  [&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;1&quot;,&quot;0&quot;],
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;],
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;]
]
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;1&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;0&quot;,&quot;0&quot;],
  [&quot;0&quot;,&quot;0&quot;,&quot;0&quot;,&quot;1&quot;,&quot;1&quot;]
]
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 300</code></li>
	<li><code>grid[i][j]</code> is <code>&#39;0&#39;</code> or <code>&#39;1&#39;</code>.</li>
</ul>

'''
Input:
List[List[str]] grid

Output:
int num_of_islands

Constraints:
- Q1: Can we modify input (to achieve In-place)? A: Please provide yes/no both two solutions.
- Q2: Which type of Directions? A: 4-directional

Test Cases:
grid = [
  ["1","1","1","1","0"],
  ["1","1","0","1","0"],
  ["1","1","0","0","0"],
  ["0","0","0","0","0"]
], return 1

grid = [
  ["1","1","0","0","0"],
  ["1","1","0","0","0"],
  ["0","0","1","0","0"],
  ["0","0","0","1","1"]
], return 3

[Solution 1: BFS + global visited set]
Idea: 
# visited = {(i, j)}
# num_of_islands = 0
# Iterate the matrix with the global visited set. 
# -- When encounter non-visited-yet 1(island), do BFS && and increment num_of_islands by 1, and also marked visited nodes.
Time: O(m * n)
Space: O(m * n) for queue and visited

[Solution 2: BFS + inplaced change visited island(1) to water(0)]
Idea:
# Follow-up by Solution 1. but discard visited. Mark seen 1 to 0 to avoid re-visit again
Time: O(m * n)
Space: O(m * n) for queue

[Solution 3: DFS + inplaced change visited island(1) to water(0)]
Idea:
# Follow-up by Solution 1. but discard visited. Mark seen 1 to 0 to avoid re-visit again
Time: O(m * n)
Space: O(m * n) for recursion space
'''


class Solution:

  def numIslands(self, grid: List[List[str]]) -> int:
    solution1 = SolutionBFSWithGlobalVisitedSet()
    solution2 = SolutionBFSInplaceChangeVistiedIslandToWater()
    solution3 = SolutionDFSInplaceChangeVistiedIslandToWater()

    return solution2.num_islands(grid)


class SolutionBFSWithGlobalVisitedSet:

  def num_islands(self, grid: List[List[str]]) -> int:
    # edge case check
    if not grid or not grid[0]:
      return 0

    # visited = {(i, j)}
    # num_of_islands = 0
    visited = set()
    num_of_islands = 0

    # Iterate the matrix with the global visited set.
    # -- When encounter non-visited-yet 1(island), do BFS && and increment num_of_islands by 1, and also marked visited nodes.
    for i in range(len(grid)):
      for j in range(len(grid[0])):
        # if it is island(1) and not visited, do bfs
        if self.is_valid(grid, i, j, visited):
          self.bfs(grid, i, j, visited)
          num_of_islands += 1

    return num_of_islands

  '''
  helpers
  '''
  WATER = "0"
  ISLAND = "1"
  DIRECTIONS = [(1, 0), (0, 1), (-1, 0), (0, -1)]

  def bfs(self, grid: List[List[str]], x: int, y: int, visited: set[(int, int)]) -> None:
    queue = collections.deque([(x, y)])
    visited.add((x, y))

    while queue:
      x, y = queue.popleft()
      for delta_x, delta_y in self.DIRECTIONS:
        next_x, next_y = x + delta_x, y + delta_y
        
        if not self.is_valid(grid, next_x, next_y, visited):
          continue

        queue.append((next_x, next_y))
        visited.add((next_x, next_y))

  def is_valid(self, grid: List[List[str]], x: int, y: int, visited: set[(int, int)]) -> bool:
    if x not in range(len(grid)) or y not in range(len(grid[0])):
      return False
    if (x, y) in visited:
      return False
    if grid[x][y] != self.ISLAND:
      return False

    return True


class SolutionBFSInplaceChangeVistiedIslandToWater:

  def num_islands(self, grid: List[List[str]]) -> int:
    # edge case check
    if not grid or not grid[0]:
      return 0

    num_of_islands = 0
    for i in range(len(grid)):
      for j in range(len(grid[0])):
        # if it is island(1) and not visited, do bfs
        if self.is_valid(grid, i, j):
          self.bfs(grid, i, j)
          num_of_islands += 1

    return num_of_islands


  '''
  helpers
  '''
  WATER = "0"
  ISLAND = "1"
  DIRECTIONS = [(1, 0), (0, 1), (-1, 0), (0, -1)]

  def bfs(self, grid: List[List[str]], x: int, y: int) -> None:
    queue = collections.deque([(x, y)])
    # Follow-up by Solution 1. but discard visited. Mark seen 1 to 0 to avoid re-visit again
    grid[x][y] = self.ISLAND

    while queue:
      x, y = queue.popleft()
      for delta_x, delta_y in self.DIRECTIONS:
        next_x, next_y = x + delta_x, y + delta_y
        
        if not self.is_valid(grid, next_x, next_y):
          continue

        queue.append((next_x, next_y))
        # Follow-up by Solution 1. but discard visited. Mark seen 1 to 0 to avoid re-visit again
        grid[next_x][next_y] = self.WATER

  def is_valid(self, grid: List[List[str]], x: int, y: int) -> bool:
    if x not in range(len(grid)) or y not in range(len(grid[0])):
      return False
    if grid[x][y] != self.ISLAND:
      return False

    return True

  
class SolutionDFSInplaceChangeVistiedIslandToWater:

  def num_islands(self, grid: List[List[int]]) -> int:
    # edge case checking
    if not grid or not grid[0]:
      return 0
    
    num_of_islands = 0
    for i in range(len(grid)):
      for j in range(len(grid[0])):
        if self.is_valid(grid, i, j):
          self.dfs(grid, i, j)
          num_of_islands += 1
      
    return num_of_islands


  '''
  helpers
  '''
  WATER = "0"
  ISLAND = "1"
  DIRECTIONS = [(1, 0), (-1, 0), (0, 1), (0, -1)]
  
  def dfs(self, grid: List[List[int]], x: int, y: int):
    # Follow-up by Solution 1. but discard visited. Mark seen 1 to 0 to avoid re-visit again
    grid[x][y] = self.WATER
    
    for delta_x, delta_y in self.DIRECTIONS:
      next_x = x + delta_x
      next_y = y + delta_y
      
      if not self.is_valid(grid, next_x, next_y):
        continue
        
      self.dfs(grid, next_x, next_y)
  
  def is_valid(self, grid: List[List[int]], x: int, y: int):
    if x not in range(len(grid)) or y not in range(len(grid[0])):
      return False
    if grid[x][y] != self.ISLAND:
      return False
    
    return True
19353