#K50892. Minimum Steps in a Grid
Minimum Steps in a Grid
Minimum Steps in a Grid
You are given an island represented as a 2D grid of size \(n \times m\), where each cell is either land or water. A cell with value 1
represents land, and a cell with value 0
represents water. You start at the top-left corner of the grid (cell (0, 0)) and your goal is to reach the bottom-right corner (cell (n-1, m-1)).
You can move only in the four cardinal directions (up, down, left, right) and you may only step on land. You cannot move diagonally or outside the grid boundaries. If it is impossible to reach the destination, output \(-1\).
Note: The positions in the grid are 0-indexed. The input is provided via standard input (stdin) and the result should be printed to standard output (stdout).
inputFormat
The first line of input contains two space-separated integers \(n\) and \(m\), representing the number of rows and columns in the grid respectively.
The next \(n\) lines each contain \(m\) space-separated integers (either 0 or 1) representing the grid.
outputFormat
Output a single integer representing the minimum number of steps required to reach the bottom-right corner from the top-left corner. If it is impossible to reach the destination, print \(-1\).
## sample3 3
1 1 0
0 1 1
1 1 1
4