Number of Islands

gridMatrix
[["1","1","0","0"],["1","1","0","0"],["0","0","1","0"],["0","0","0","1"]]
1def numIslands(grid):
2  if not grid:
3    return 0
4
5  rows, cols = len(grid), len(grid[0])
6  count = 0
7  directions = [(1,0), (-1,0), (0,1), (0,-1)]
8
9  for row in range(rows):
10    for col in range(cols):
11
12      if grid[row][col] == '1':
13        count += 1
14
15        # BFS to mark entire island
16        queue = [(row, col)]
17        grid[row][col] = 'V'  # Mark as visited
18
19        while queue:
20          curr_row, curr_col = queue.pop(0)
21
22          # Check neighbors in S, N, E, W order
23          for dr, dc in directions:
24            new_row = curr_row + dr
25            new_col = curr_col + dc
26
27            if 0 <= new_row < rows and 0 <= new_col < cols:
28              if grid[new_row][new_col] == '1':
29                grid[new_row][new_col] = 'V'
30                queue.append((new_row, new_col))
31
32  return count
0 / 63
1100110000100001queue