<p>You are given an <code>m x n</code> <code>grid</code> where each cell can have one of three values:</p>

<ul>
	<li><code>0</code> representing an empty cell,</li>
	<li><code>1</code> representing a fresh orange, or</li>
	<li><code>2</code> representing a rotten orange.</li>
</ul>

<p>Every minute, any fresh orange that is <strong>4-directionally adjacent</strong> to a rotten orange becomes rotten.</p>

<p>Return <em>the minimum number of minutes that must elapse until no cell has a fresh orange</em>. If <em>this is impossible, return</em> <code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2019/02/16/oranges.png" style="width: 650px; height: 137px;" />
<pre>
<strong>Input:</strong> grid = [[2,1,1],[1,1,0],[0,1,1]]
<strong>Output:</strong> 4
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> grid = [[2,1,1],[0,1,1],[1,0,1]]
<strong>Output:</strong> -1
<strong>Explanation:</strong> The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> grid = [[0,2]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> Since there are already no fresh oranges at minute 0, the answer is just 0.
</pre>

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

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 10</code></li>
	<li><code>grid[i][j]</code> is <code>0</code>, <code>1</code>, or <code>2</code>.</li>
</ul>

class Solution:
    def orangesRotting(self, grid: List[List[int]]) -> int:
        # multi source bfs
        m,n = len(grid),len(grid[0])
        queue = []
        start,stop = 0,-1
        flag = True
        # matrix = [[0 for i in range(n)] for j in range(m)]
        for i in range(m):
            for j in range(n):
                if grid[i][j]!=0:
                    flag = False
                if grid[i][j]==2:
                    queue.append([i,j])
                    # matrix[i][j] = -1
                    stop += 1

        def mark(i,j):
            if i-1>=0 and grid[i-1][j]==1:
                grid[i-1][j] = 2
                queue.append([i-1,j])
            if j-1>=0 and grid[i][j-1]==1:
                grid[i][j-1] = 2
                queue.append([i,j-1])
            if i+1<m and grid[i+1][j]==1:
                grid[i+1][j] = 2
                queue.append([i+1,j])
            if j+1<n and grid[i][j+1]==1:
                grid[i][j+1] = 2
                queue.append([i,j+1])

        time = 0
        if flag:
            return 0
        while start < len(queue):
            element = queue[start]
            mark(element[0],element[1])
            start += 1
            if start > stop:
                stop = len(queue)-1
                time += 1
                # print(queue,grid) 

        for i in range(m):
            for j in range(n):
                if grid[i][j]==1:
                    return -1
        return time-1
16100