<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>

class Solution:
    def maxDistance(self, grid: List[List[int]]) -> int:
        R,C = len(grid),len(grid[0])
        q = deque()
        visited = set()
        for r in range(R):
            for c in range(C):
                if grid[r][c] == 1:
                    q.append((r,c))
                    visited.add((r,c))
        time = 0
        while q:
            for i in range(len(q)):
                r,c = q.popleft()
                for dr,dc in [[1,0],[-1,0],[0,1],[0,-1]]:
                    rr,cc = r+dr,c+dc
                    if rr<0 or cc<0 or rr>=R or cc>=C or grid[rr][cc] == 1 or (rr,cc) in visited:
                        continue
                    q.append((rr,cc))
                    visited.add((rr,cc))
                    grid[rr][cc] =1
            time+=1
        return time -1 if time>1 else -1
            


















        R,C = len(grid),len(grid[0])
        visited = set()
        q = deque()
        for r in range(R):
            for c in range(C):
                if grid[r][c] == 1:
                    q.append((r,c))
                    visited.add((r,c))
        res = 0
        while q:
            for i in range(len(q)):
                rr,cc = q.popleft()
                for dr,dc in  [[1,0],[-1,0],[0,-1],[0,1]]:
                    r,c =rr+dr,cc+dc
                    if r<0 or c<0 or r>=R or c>=C or grid[r][c]!=0:continue
                    grid[r][c] =1
                    q.append((r,c))
                    visited.add((r,c))
            res+=1
        return res-1 if res>1 else -1
















        R,C = len(grid),len(grid[0])
        q = deque()
        for r in range(R):
            for c in range(C):
                if grid[r][c] == 1:
                    q.append((r,c))
        visited = set()
        step = 0
        while q:
            for i in range(len(q)):
                r,c = q.popleft()
                
                for dr,dc in [[1,0],[-1,0],[0,1],[0,-1]]:
                    rr,cc = r+dr,c+dc
                    if rr<0 or cc<0 or rr>=R or cc>=C or grid[rr][cc]==1 or (rr,cc) in visited:
                        continue
                    visited.add((rr,cc))
                    q.append((rr,cc))
            step+=1
        return step-1 if step>1 else -1
                     




















        R,C = len(grid),len(grid[0])
        q = deque()
        for r in range(R):
            for c in range(C):
                if grid[r][c] == 1:
                    q.append((r,c))
        visited = set()
        step = 0
        while q:
            for i in range(len(q)):
                r,c = q.popleft()
                for dr,dc in [[1,0],[0,1],[-1,0],[0,-1]]:
                    rr,cc = r+dr,c+dc
                    if rr<0 or cc<0 or rr>=R or cc>=C or grid[rr][cc]!=0 or (rr,cc) in visited:continue 
                    grid[rr][cc] = 1
                    visited.add((rr,cc))
                    q.append((rr,cc))
            step+=1
        return step-1 if visited else -1






















        R,C = len(grid),len(grid[0])
        visited = set()
        q = deque()
        for r in range(R):
            for c in range(C):
                if grid[r][c] == 1:
                    q.append((r,c))
                    visited.add((r,c))
        step = 0
        while q:
            for i in range(len(q)):
                r,c = q.popleft()
                for dr,dc in [[1,0],[-1,0],[0,1],[0,-1]]:
                    rr,cc = r+dr,c+dc
                    if rr<0 or cc<0 or rr>=R or cc>=C or grid[rr][cc]!=0 or (rr,cc) in visited:
                        continue
                    q.append((rr,cc))
                    visited.add((rr,cc))
            step+=1
        return step-1 if step>1 else -1




















        R,C = len(grid),len(grid[0])
        q = deque()
        for r in range(R):
            for c in range(C):
                if grid[r][c] == 1:
                    q.append((r,c))
        visited = set()
        step = 0
        while q:
            for i in range(len(q)):
                r,c = q.popleft()
                for dr,dc in [[1,0],[-1,0],[0,1],[0,-1]]:
                    rr,cc = r+dr,c+dc
                    if rr<0 or cc<0 or rr>=R or cc>=C or (rr,cc) in visited or grid[rr][cc]!=0:continue
                    visited.add((rr,cc))
                    q.append((rr,cc))
            step+=1
        return step-1 if step>1 else -1 
        

















        q = deque()
        R,C = len(grid),len(grid[0])
        for r in range(R):
            for c in range(C):
                if grid[r][c] == 1:
                    q.append((r,c))
        # if not q:return -1
        visited = set()
        step = 0
        while q:
            for i in range(len(q)):
                rr,cc = q.popleft()
                for dr,dc in [[1,0],[-1,0],[0,1],[0,-1]]:
                    r,c = rr+dr,cc+dc
                    if r<0 or c<0 or r>=R or c>=C or (r,c) in visited or grid[r][c]!=0:
                        continue
                    visited.add((r,c))
                    q.append((r,c))
            step+=1
        
        return step-1 if step>1 else -1
























        R,C = len(grid),len(grid[0])
        q = deque()
        for r in range(R):
            for c in range(C):
                if grid[r][c] == 1:
                    q.append((r,c))
        step = 0
        visited = set()
        while q:
            for i in range(len(q)):
                rr,cc = q.popleft()
                for dr,dc in [[1,0],[-1,0],[0,1],[0,-1]]:
                    r,c = rr+dr,cc+dc
                    if r<0 or c<0 or c>=C or r>=R or grid[r][c]!=0 or (r,c) in visited:
                        continue
                    grid[r][c] =1
                    q.append((r,c))
                    visited.add((r,c))
            step+=1
        return step-1 if step>1 else -1





























        R,C = len(grid),len(grid[0])
        q = deque()
        visited = set()
        for r in range(R):
            for  c in range(C):
                if grid[r][c]==1:
                    q.append((r,c))
                    visited.add((r,c))
        res = -1
        while q:
            for i in range(len(q)):
                r,c  = q.popleft()
                res = grid[r][c]
                for dr,dc in [[1,0],[-1,0],[0,1],[0,-1]]:
                    rr,cc = r+dr,c+dc
                    if rr<0 or cc<0 or rr>=R or cc>=C or grid[rr][cc] != 0 or (rr,cc) in visited:
                        continue
                    grid[rr][cc] = 1+ grid[r][c]
                    q.append((rr,cc))
                    visited.add((rr,cc))
        return res-1 if res>1 else -1

                    































        R,C = len(grid),len(grid[0])
        q = deque()
        visited = set()
        for r in range(R):
            for c in range(C):
                if grid[r][c]==1:
                    q.append((r,c))
                    visited.add((r,c))
        step = 0
        while q:
            
            for i in range(len(q)):
                rr,cc = q.popleft()
                for dr,dc in [[1,0],[-1,0],[0,1],[0,-1]]:
                    r,c = rr+dr,cc+dc
                    if r<0 or c<0 or r>=R or c>=C or grid[r][c]!=0 or (r,c) in visited:
                        continue
                    if grid[r][c]==1:
                        return steps
                    q.append((r,c))
                    visited.add((r,c))
                    grid[r][c] = 1
            step+=1
        return step-1 if step>0 else 0
        

        























        R,C = len(grid),len(grid[0])
        q = deque()
        visited = set()
        for r in range(R):
            for c in range(C):
                if grid[r][c] == 1:
                    q.append([r,c])
                    visited.add((r,c))
        dire = [[1,0],[-1,0],[0,1],[0,-1]]

        res = -1
        
        while q:
            for i in range(len(q)):
                dr,dc = q.popleft()
                res = grid[dr][dc]
                for row,col in dire:
                    r,c = row+dr,col+dc
                    if r<0 or c<0 or r>=R or c>=C or grid[r][c]!=0 or (r,c) in visited:
                        continue
                    grid[r][c] = 1+grid[dr][dc]
                    visited.add((r,c))
                    q.append((r,c))
        return  res-1 if res>1 else   -1
            







































        dire = [[1,0],[-1,0],[0,1],[0,-1]]
        R,C = len(grid),len(grid[0])
        q = deque()
        visited = set()
        for r in range(R):
            for c in range(C):
                if grid[r][c] == 1:
                    q.append([r,c])
                    visited.add((r,c))
        # visited = set()
        res = -1
        while q:
            for i in range(len(q)):
                row,col  = q.popleft()
                res = grid[row][col]
                for dr, dc in dire:
                    r,c = row+dr,col+dc
                    if r<0 or r>=R or c<0 or c>=C or (r,c) in visited or grid[r][c]!=0:
                        continue
                    grid[r][c] = 1+grid[row][col]
                    q.append([r,c])
                    visited.add((r,c))
        return res-1 if res>1 else -1
        



































        dire = [[1,0],[-1,0],[0,1],[0,-1]]
        R,C = len(grid),len(grid[0])
        q = deque()
        for r in range(R):
            for c in range(C):
                if grid[r][c] == 1:
                    q.append([r,c])
        res = -1
                    
        if len(q)==0:return -1
    
        visited = set()
        while q:
            for i in range(len(q)):
                row,col = q.popleft()
                res = grid[row][col]
                for dr,dc in dire:
                    rr,cc = row+dr,col+dc
                    if rr<0 or cc<0 or rr>=R or cc>=C or grid[rr][cc]!=0 or (rr,cc)  in visited:
                        continue
                    grid[rr][cc] = grid[row][col]+1
                    visited.add((rr,cc))
                    q.append([rr,cc])
            
        return res-1 if res>1 else -1






        
                    
412