As Far from Land as Possible LeetCode solution in Python
1from collections import *234class Solution:5def maxDistance(self, grid) -> int:6m, n = len(grid), len(grid[0])7queue = deque([])8for i in range(m):9for j in range(n):10if grid[i][j] == 1:11queue.append((i, j))12c = -113while queue:14# print(queue)15temp = len(queue)16for _ in range(temp):17(i, j) = queue.popleft()18for x, y in ((i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)):19if x < 0 or x >= m or y < 0 or y >= n or grid[x][y] == 1:20continue21grid[x][y] = 122queue.append((x, y))23c += 124return c if c != 0 else -1