You are given an m x n grid where each cell is one of:
  • 0: empty cell
  • 1: a fresh orange
  • 2: a 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
Inputgrid = [[2,1,1],[1,1,0],[0,1,1]]
Output4
Example 2
Inputgrid = [[2,1,1],[0,1,1],[1,0,1]]
Output-1
ExplanationThe orange in the bottom left corner (1,0) is never rotten, because rotting only happens 4-directionally.
Example 3
Inputgrid = [[0,2]]
Output0
ExplanationSince there are already no fresh oranges at minute 0, the answer is just 0.