#K51487. Shortest Path in a Maze
Shortest Path in a Maze
Shortest Path in a Maze
You are given a maze represented as a 2D grid of size \(n \times m\). Each cell in the grid is either a wall (represented by 1) or a path (represented by 0). Your task is to determine the shortest path from the top-left corner (0,0) to the bottom-right corner (n-1, m-1). You may move in four directions: up, down, left, or right. The length of the path is defined as the number of cells visited along the route, including both the starting and the ending cell. If no valid path exists, output \(-1\).
inputFormat
The input is provided via standard input (stdin). The first line contains two integers \(n\) and \(m\), representing the number of rows and columns in the maze respectively. This is followed by \(n\) lines, each containing \(m\) space-separated integers (either 0 or 1) representing the maze grid.
outputFormat
Output the length of the shortest path found. If there is no possible path, output \(-1\).
## sample5 5
0 1 0 0 0
0 1 0 1 0
0 0 0 1 0
0 1 1 1 0
0 0 0 0 0
9