"""
In a given grid, each cell can have one of three values:

the value 0 representing an empty cell;
the value 1 representing a fresh orange;
the value 2 representing a rotten orange.
Every minute, any fresh orange that is adjacent (4-directionally) to a rotten orange becomes rotten.

Return the minimum number of minutes that must elapse until no cell has a fresh orange.  If this is impossible, return -1 instead.
"""
class Solution:
    def orangesRotting(self, grid: List[List[int]]) -> int:
        counter = -1
        lenX, lenY = len(grid), len(grid[0])
        rot = True
        while rot:
            print(grid, rot)
            changed = False
            for i in range(lenX):
                for j in range(lenY):
                    if grid[i][j] == 2:
                        changed = rotFreshNeighbours(i, j, grid) or changed
            rot = changed
            counter += 1
        for i in range(lenX):
            for j in range(lenY):
                if grid[i][j] == 1:
                    return -1
        return counter


def rotFreshNeighbours(x, y: int, grid: List[List[int]]) -> bool:
    rotted = False
    neighbours = [[x+1, y], [x-1, y], [x, y+1], [x, y-1]]
    for coords in neighbours:
        nX, nY = coords
        if nX < 0 or nX >= len(grid):
            continue
        if nY < 0 or nY >= len(grid[0]):
            continue
        if grid[nX][nY] == 1:
            rotted = True
            grid[nX][nY] = 2
    return rotted

@@ -507,30 +507,8 @@
 t:%0D%0A
-        counter = -1%0D%0A
     
@@ -561,97 +561,127 @@
     
-rot = True%0D%0A        while rot:%0D%0A            print(grid, rot)%0D%0A            changed = False
+counter = 0%0D%0A        rottable = %5B%5D%0D%0A        for i, row in enumerate(grid):%0D%0A            for j, value in enumerate(row):
 %0D%0A  
@@ -694,304 +694,396 @@
     
-for i in range(lenX):%0D%0A                for j in range(lenY):%0D%0A                    if grid%5Bi%5D%5Bj%5D == 2:%0D%0A                        changed = rotFreshNeighbours(i, j, grid) or changed%0D%0A            rot = changed%0D%0A            counter += 1%0D%0A        for i in range(lenX):%0D%0A            for j in range(lenY)
+    if value == 2:%0D%0A                    rottable.append(%5Bi, j%5D)%0D%0A        while rottable:%0D%0A            nextRot = %5B%5D%0D%0A            for coords in rottable:%0D%0A                nextRot.extend(getRottableNeighbours(coords%5B0%5D, coords%5B1%5D, lenX, lenY, grid))%0D%0A            rottable = nextRot%0D%0A            for coords in rottable:%0D%0A                grid%5Bcoords%5B0%5D%5D%5Bcoords%5B1%5D%5D = 2%0D%0A            if rottable
 :%0D%0A 
@@ -1101,27 +1101,22 @@
     
-if grid%5Bi%5D%5Bj%5D == 1:
+counter += 1%0D%0A
 %0D%0A  
@@ -1121,28 +1121,63 @@
         
-     
+if any(1 in row for row in grid):%0D%0A
        
+     
 return -
@@ -1215,16 +1215,19 @@
 def 
-rotFresh
+getRottable
 Neig
@@ -1237,16 +1237,28 @@
 urs(x, y
+, lenX, lenY
 : int, g
@@ -1286,33 +1286,24 @@
  -%3E 
-bool:%0D%0A    rotted = False
+List%5BList%5Bint%5D%5D:
 %0D%0A  
@@ -1359,16 +1359,35 @@
  y-1%5D%5D%0D%0A
+    rottable = %5B%5D%0D%0A
     for 
@@ -1464,22 +1464,17 @@
 X %3E= len
-(grid)
+X
 :%0D%0A     
@@ -1520,25 +1520,17 @@
 Y %3E= len
-(grid%5B0%5D)
+Y
 :%0D%0A     
@@ -1597,47 +1597,29 @@
 rott
-ed = True%0D%0A            grid%5BnX%5D%5BnY%5D = 2
+able.append(%5BnX, nY%5D)
 %0D%0A  
@@ -1635,8 +1635,10 @@
 rott
+abl
 e
-d
 %0D%0A

cut down on computations