Assume that you are given a two-dimensional grid \(G\) that contains \(N\) rows and \(M\) columns. The grid \(G\) consists of only three integers (\(0\), \(1\), and \(2\)).
You can move into an adjacent cell if that adjacent cell is empty. Two cells are adjacent if they share a side. In other words, if a cell is empty, then you can move in one of the four directions, Up, Down, Left, and Right.
You cannot move out of the grid \(G\).
Your task is to find the length of the shortest path to reach one of the boundary edges of the grid without stepping on a plant. The length of a path is equal to the number of moves you make.
Note: It is guaranteed that there is only one cell with value \(2\) (you are standing in only one cell initially).
Input format
Output format
Print the length of the shortest path to reach one of the boundary edges of the grid without stepping on a plant. If it is not possible to reach the edge of the grid, then print \(-1\).
Constraints
\(1 \le N, M \le 100\)
\(0 \le G_{i, j} \le 2\) where \(G_{i,j}\) denotes a cell of the \(G\) grid
There are 4 rows and 5 columns in the grid.
1 1 1 0 1
1 0 2 0 1
0 0 1 0 1
1 0 1 1 0
Initially, you are standing at cell \((2, 3)\) (second row and third column) denoted by 2. There are three ways to reach an boundary edge of the grid without stepping on a plant.
In the third step, we took only 2 steps to reach to the edge of the grid which is the shortest path.