You are given an n * m grid where each position can contain one of the three values:
| Cell Value | Represents |
|---|---|
| 0 | Empty Cell |
| 1 | Fresh Apple |
| 2 | Rotten Apple |
Every day, all fresh apples which are adjacent to any rotten apple become rotten.
Two cells are adjacent if they have a common edge, i.e., each cell can have upto four adjacent cells.
Find the minimum number of days required for all the apples to be rotten. If this is not possible return -1.
The first line contains an integer ‘T’ denoting the number of independent test cases.
For each test case the input has the following lines:
For each test case, a line containing the minimum number of days for all the apples to be rotten.
If it is impossible, the value is -1.
2
3 3
2 1 0
1 1 0
0 1 1
3 3
2 1 1
1 1 0
1 0 1
4
-1
1 <= T <= 100
1 <= n, m <= 200
0 <= cell value <= 2