Description

You are given an m x n grid where each cell can have: 0 (empty), 1 (fresh orange), or 2 (rotten orange). Every minute, any fresh orange adjacent to a rotten one becomes rotten. Return the minimum number of minutes until no fresh orange remains, or -1 if impossible.

Examples

Input:grid = [[2,1,1],[1,1,0],[0,1,1]]
Output:4
Explanation:

After 4 minutes, all oranges are rotten.

Input:grid = [[0,2]]
Output:0
Explanation:

Edge case returning zero.

Input:grid = [[1,1,0],[1,2,1],[0,1,1]]
Output:2
Explanation:

The rotten orange at position (1,1) spreads to adjacent fresh oranges. After 1 minute, oranges at (0,1), (1,0), and (1,2) become rotten. After 2 minutes, the remaining fresh oranges at (0,0), (2,1), and (2,2) become rotten. All oranges are rotten after 2 minutes.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m, n ≤ 10

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!