Amazon interview question

Rotten Oranges problem from leetcode