Given an m×n grid: 0=empty, 1=fresh, 2=rotten. Every minute, fresh oranges 4-adjacent to rotten become rotten. Return minimum minutes until no fresh remains, or -1 if impossible.
[[2,1,1],[1,1,0],[0,1,1]]4All rotten oranges start simultaneously. Each BFS level = 1 minute. Fresh neighbors become rotten when visited. If fresh>0 after BFS ends, some oranges are unreachable → return -1.
You could rescan the whole grid every minute, looking for oranges next to rotten ones. That repeats a lot of work, up to O of rows times columns per minute.
All rotten oranges start spreading at the same time. So put every rotten orange into the queue at the start, which is a multi source BFS. Process the queue one level at a time, and each level is one minute. Count the fresh oranges first, and decrease the count whenever one rots. If fresh oranges remain at the end, some were unreachable, so return −1.
Many starting points at once means multi source BFS. One BFS level equals one unit of time.
Run the animation above on your own input, or watch the 1:60 video explanation of Rotting Oranges with a dry run and Java code.