#C9633. Shortest Path in a Binary Grid
Shortest Path in a Binary Grid
Shortest Path in a Binary Grid
You are given a binary grid of dimensions \( n \times m \) where each cell contains either a 0 or a 1. A cell with 0 represents a free space and a cell with 1 represents an obstacle. Your task is to find the length of the shortest path from the top-left cell (\(1,1\)) to the bottom-right cell (\(n,m\)). You cannot move diagonally; you may only move up, down, left, or right. If there is no valid path, output \(-1\).
Note: The length is defined as the number of moves required to reach the destination. If the grid consists of a single cell, the answer is 0.
inputFormat
The input is given via stdin in the following format:
n m row1 row2 ... rown
Where:
- \( n \) and \( m \) are two integers representing the number of rows and columns, respectively.
- Each of the following \( n \) lines contains \( m \) integers (either 0 or 1) separated by spaces, representing a row of the grid.
outputFormat
Output a single integer via stdout representing the length of the shortest path. If no valid path exists, output \(-1\).
## sample3 3
0 1 0
0 0 0
0 1 0
4
</p>