Given a grid containing fresh oranges, rotten oranges, and empty cells, return the minimum number of minutes needed until no fresh orange remains. Each minute, a rotten orange infects its four-directional neighbors. Pattern focus: BFS. Run a multi-source BFS from all rotten oranges simultaneously.
grid = 2D integer matrix
minimum minutes to rot all oranges
Example 1:
Input:
grid = [[2,1,1],[1,1,0],[0,1,1]]
Output:
4
Explanation:
All fresh oranges rot after 4 minutes.
Example 2:
Input:
grid = [[2,1,1],[0,1,1],[1,0,1]]
Output:
-1
Explanation:
Some fresh oranges can never be reached.
Example 3:
Input:
grid = [[0,2]]
Output:
0
Explanation:
There are no fresh oranges to rot.