You are given an m x n grid where each cell can have one of three values:
0- empty cell1- fresh orange2- rotten orange
Every minute, any fresh orange that is 4-directionally adjacent 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.
Examples
Example 1
Input: grid = [
[2,1,1], [1,1,0], [0,1,1] ] Output: 4 All oranges rot in 4 minutes as the rot spreads from (0,0) outward.
Example 2
Input: grid = [
[2,1,1], [0,1,1], [1,0,1] ] Output: -1 The orange at bottom-left can never rot (isolated by empty cells).
Example 3
Input: grid = [[0,2]]
Output: 0
No fresh oranges, so 0 minutes needed.
Constraints
m == grid.lengthn == grid[i].length1 <= m, n <= 10grid[i][j]is0,1, or2