class Solution:
def numIslands(self, grid) -> int:
M, N = len(grid), len(grid[0])
def dfs(m, n):
r = 0 <= m < M and 0 <= n < N and grid[m][n] == '1'
if r:
grid[m][n] = None
list(map(dfs, (m + 1, m - 1, m, m), (n, n, n + 1, n - 1)))
return r
return sum(dfs(m, n) for m in range(M) for n in range(N))
200. Number of Islands - LeetCode
ยท One min read