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.
Every minute, each rotten orange rots the fresh oranges right next to it (up, down, left and right). How many minutes until none is fresh? Or −1 if some orange never rots.
One pass over the grid counts the fresh oranges and puts EVERY rotten one in the queue. They all start at once: this is breadth-first search from many sources.
Each pass of the loop pops exactly the oranges that were in the queue when the minute began. Each rots its fresh neighbours, which join the queue for the next minute.
An orange reached by two rotten neighbours in the same minute rots once: the second check finds it already rotten and skips it. No visited set is needed: the grid value is the mark.
Rot travels only through touching oranges. An orange walled off by empty cells (or the crate edge) is never reached; the queue runs dry while fresh > 0, so the answer is −1.