A forest is a grid with R rows and C columns. Each cell is one of:
0- bare ground that never burns,1- unburnt vegetation,2- a cell that is already on fire at minute 0.
Every minute, each burning cell sets fire to its unburnt-vegetation neighbors in the four cardinal directions (up, down, left, right); those neighbors become burning at the start of the next minute. Bare-ground cells (0) never burn.
Report the number of minutes that elapse until no unburnt vegetation remains. If at least one vegetation cell can never catch fire, report -1.
Input format
Line 1: two integers R and C.
Next R lines: each has C space-separated integers (0, 1, or 2).
Output format
A single integer: the minutes until all vegetation is burning, or -1 if impossible.
Constraints
- 1 <= R, C <= 500
- Each cell is 0, 1, or 2.