We’re preparing your current view and syncing the latest data.
You are given an m x n grid where each cell can have one of three values: 0 representing an empty cell, 1 representing a fresh orange, and 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 it is impossible for all the fresh oranges to rot, return -1.
An m x n integer grid where each element is 0, 1, or 2.
An integer representing the minimum minutes to rot all oranges, or -1 if impossible.
1 <= m, n <= 10^2 grid[i][j] is 0, 1, or 2.
Example 1
Input
[[2,1,1],[1,1,0],[0,1,1]]
Output
4
Explanation
The grid evolves as follows: Minute 0: [[2,1,1],[1,1,0],[0,1,1]] Minute 1: [[2,2,1],[2,1,0],[0,1,1]] Minute 2: [[2,2,2],[2,2,0],[0,1,1]] Minute 3: [[2,2,2],[2,2,0],[0,2,1]] Minute 4: [[2,2,2],[2,2,0],[0,2,2]] All oranges rotten at minute 4.