(matrix: List[List[int]])
| 2 | |
| 3 | |
| 4 | def count_islands(matrix: List[List[int]]) -> int: |
| 5 | if not matrix: |
| 6 | return 0 |
| 7 | count = 0 |
| 8 | for r in range(len(matrix)): |
| 9 | for c in range(len(matrix[0])): |
| 10 | # If a land cell is found, perform DFS to explore the full |
| 11 | # island, and include this island in our count. |
| 12 | if matrix[r][c] == 1: |
| 13 | dfs(r, c, matrix) |
| 14 | count += 1 |
| 15 | return count |
| 16 | |
| 17 | def dfs(r: int, c: int, matrix: List[List[int]]) -> None: |
| 18 | # Mark the current land cell as visited. |