Watch rot spread through a crate of oranges from every rotten one at once, one BFS level per minute, and see why an orange behind empty cells never rots (-1). O(R × C) time and space.
About this algorithm
A crate holds fresh oranges, rotten ones and empty cells. Every minute, each rotten orange rots the fresh oranges directly above, below, left and right of it. The question is how many minutes pass until no fresh orange is left, or -1 if some can never rot. It is breadth-first search that starts from every rotten orange at the same time. This is LeetCode 994.
Use multi-source BFS whenever several starting points spread at the same speed and you want the time until everything is reached: fire from several sparks, the distance from every room to its nearest gate (Walls and Gates), or 01 Matrix. Running a separate BFS from each rotten orange would cost a full search per source and still need merging; putting them all in the first queue does the job in one pass. The grid BFS page has one start and one target; here there are many starts, no target, and the answer is the number of rounds.
Count the fresh oranges and put every rotten orange in the queue. They all belong to minute 0.
While the queue has oranges and fresh ones remain, add one to the minute counter and take exactly the oranges that were queued when the minute began. Each checks up, down, left and right.
A fresh neighbour turns rotten at once, the fresh count drops by one, and it joins the queue for the next minute. A second rotten orange reaching it in the same minute finds it already rotten, so the grid value doubles as the visited mark.
If the fresh count reaches 0, the minute counter is the answer. If the queue empties first, some fresh orange is cut off by empty cells or the crate edge, and the answer is -1.
The default crate has rotten oranges in three corners, top-left, bottom-left and bottom-right, with 30 fresh oranges and 3 empty cells. Minute 1 rots 6 oranges, 2 next to each corner. Minutes 2, 3 and 4 rot 9, 8 and 5 more, and minute 5 rots one last orange at row 1, column 4 (counting from 0), reached from two sides at once. One fresh orange is left, in the top-right corner. The empty cells to its left and below it shut it in, and its only other neighbour is diagonal, which does not count. Minute 6 starts from that last rotten orange, rots nothing, and the queue runs dry with 1 fresh orange left, so the answer is -1. Generate New cycles through three more crates; in two of them every orange rots, after 4 minutes.
The usual bug is adding a minute per orange instead of per round, which a fixed queue length at the start of each round prevents. The other is counting one round too many at the end; stopping the loop as soon as the fresh count hits 0, as this page does, returns exactly the minute the last orange turned.
Time: the first scan reads every cell once, and each orange enters the queue at most once and checks 4 neighbours. Space: one minute's queue can hold a large share of the oranges; the grid itself serves as the visited marks.