#C530. Shortest Path to the Campsite

    ID: 48934 Type: Default 1000ms 256MiB

Shortest Path to the Campsite

Shortest Path to the Campsite

You are given a grid of size \(n \times m\), where each cell is either walkable (represented by 0) or blocked (represented by 1). Starting at the top-left cell (\(0,0\)) and trying to reach the bottom-right cell (\(n-1,m-1\)), you can move one step at a time in four directions: up, down, left, or right. The task is to determine the minimum number of steps required to reach the campsite.

If the starting cell or the destination is blocked, or if there is no valid path, then output \(-1\). Note that if the grid consists of only one cell, you should output \(0\) since no movement is necessary.

inputFormat

The input is read from standard input and is formatted as follows:

  • The first line contains two integers \(n\) and \(m\), representing the number of rows and columns respectively.
  • The next \(n\) lines each contain \(m\) integers (each either 0 or 1) separated by spaces, representing the grid.

outputFormat

Output a single integer to standard output: the minimum number of steps required to reach the bottom-right corner from the top-left corner, or \(-1\) if it is not possible.

## sample
3 3
0 0 0
1 0 1
1 0 0
4