<p>Given an <code>n x n</code> <code>grid</code>&nbsp;containing only values <code>0</code> and <code>1</code>, where&nbsp;<code>0</code> represents water&nbsp;and <code>1</code> represents land, find a water cell such that its distance to the nearest land cell is maximized, and return the distance.&nbsp;If no land or water exists in the grid, return <code>-1</code>.</p>

<p>The distance used in this problem is the Manhattan distance:&nbsp;the distance between two cells <code>(x0, y0)</code> and <code>(x1, y1)</code> is <code>|x0 - x1| + |y0 - y1|</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/05/03/1336_ex1.JPG" style="width: 185px; height: 87px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,1],[0,0,0],[1,0,1]]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The cell (1, 1) is as far as possible from all the land with distance 2.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/05/03/1336_ex2.JPG" style="width: 184px; height: 87px;" />
<pre>
<strong>Input:</strong> grid = [[1,0,0],[0,0,0],[0,0,0]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The cell (2, 2) is as far as possible from all the land with distance 4.
</pre>

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

<ul>
	<li><code>n == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= n&nbsp;&lt;= 100</code></li>
	<li><code>grid[i][j]</code>&nbsp;is <code>0</code> or <code>1</code></li>
</ul>

"""
   /0 1 2/
0 |0 0 1
1 |0 0 0
2 |1 0 1

0,2   2,0

0 0 1
0 0 1
0 0 1

2 1 2 2 1 2
1 X 1 1 X 1
2 1 2 2 1 2

2 1 2 2 1 2
1  1 1 X 1
2 1 2 2 1 2

0 0 0 0 
0 0 0 0
0 1 0 0
0 0 0 0 
0 0 0 0
1 0 0 0
0 0 0 0 
0 0 0 0
0 0 0 0
0 0 0 0 
0 0 1 0
0 0 0 0

0 3 0 3 
3 2 3 2
2 1 2 1
3 2 3 2 
2 3 0 3
1 2 3 4
2 3 4  
0 0 4 0
0 4 3 4
4 3 2 3 
3 2 1 2
4 3 2 3


"""
from collections import deque

class Solution:
    def maxDistance(self, grid: List[List[int]]) -> int:
        to_explore = deque([])
        n = len(grid)
        for i in range(n):
            for j in range(n):
                if grid[i][j] == 1:
                    to_explore.append([i, j])
        maxi = -1
        while to_explore:
            i, j = to_explore.pop()
            #i - 1, j
            if i - 1 >= 0 and grid[i - 1][j] == 0:
                grid[i - 1][j] = grid[i][j] + 1
                maxi = max(maxi, grid[i - 1][j])
                to_explore.appendleft([i - 1, j])
            #i + 1, j
            if i + 1  <n and grid[i + 1][j] == 0:
                grid[i + 1][j] = grid[i][j] + 1
                maxi = max(maxi, grid[i + 1][j])
                to_explore.appendleft([i + 1, j])
            #i, j-1,
            if j - 1 >= 0 and grid[i][j - 1] == 0:
                grid[i][j - 1] = grid[i][j] + 1
                maxi = max(maxi, grid[i][j - 1])
                to_explore.appendleft([i, j - 1])
            #i, j + 1
            if j + 1 <n and grid[i][j + 1] == 0:
                grid[i][j + 1] = grid[i][j] + 1
                maxi = max(maxi, grid[i][j + 1])
                to_explore.appendleft([i, j + 1])
        if maxi != -1:
            return maxi - 1
        return maxi
322