Problem of the day
Input: [[2,1,1],[1,1,0],[0,0,0]]
Output: 2
At T=0, only orange at (0,0) is rotten.
At T=1, oranges at (0,0),(0,1) and (1,0) are rotten.
At T=2, oranges at (0,0),(0,1),(1,0),(0,2) and (1,1) are rotten.
No fresh oranges are left after T=2.
The first line will contain the integer 'T', denoting the number of test cases.
The first line of each test case contains two single space-separated integers, 'N' and 'M', representing the grid's number of rows and columns, respectively.
The next 'N' lines contain 'M' single space-separated integers, each representing the rows of the grid.
The only line of output contains a single integer, i.e., The minimum time after which no cell has a fresh orange.
If it's impossible to rot all oranges, print -1.
You are not required to print the expected output. It has already been taken care of. Just implement the function.
1 <= N*M <= 10^5
0 <= grid[i][j] <= 2
Time Limit: 1 sec
2
3 3
2 1 0
1 1 0
0 0 0
2 2
2 1
1 2
2
1
For the first case:
Input: [[2,1,1],[1,1,0],[0,0,0]]
Output: 2
At T=0, only orange at (0,0) is rotten.
At T=1, oranges at (0,0),(0,1) and (1,0) are rotten.
At T=2, oranges at (0,0),(0,1),(1,0),(0,2) and (1,1) are rotten.
No fresh oranges are left after T=2.
For the second case:
Input: [[2,1],[1,2]]
Output: 1
At T=0, only oranges at (0,0) and (1,1) are rotten.
At T=1, oranges at (0,0),(0,1),(1,0) and (1,1) are rotten.
No fresh oranges are left after T=1.
2
3 3
2 1 1
1 1 0
0 1 1
3 3
2 1 0
0 1 1
1 0 1
4
-1