#K82397. Minimum Moves to Reach Destination

    ID: 35966 Type: Default 1000ms 256MiB

Minimum Moves to Reach Destination

Minimum Moves to Reach Destination

You are given an \( n \times m \) grid where each cell is either . representing an empty space or # representing an obstacle. You need to find the minimum number of moves required to travel from the top-left corner (\(0,0\)) to the bottom-right corner (\(n-1, m-1\)). You can only move in the four cardinal directions: up, down, left, or right, and you cannot step on cells with obstacles.

If it is not possible to reach the destination, output \(-1\).

Note: The starting cell and the destination cell will always be provided in the input. However, they might be blocked by obstacles, which makes the journey impossible.

inputFormat

The input is given via standard input (stdin) and consists of:

  1. An integer pair \(n\) and \(m\) representing the number of rows and columns.
  2. Then \(n\) lines follow, each containing a string of length \(m\), which represents a row of the grid. Each character is either . or #.

For example:

4 4
....
.##.
....
....

outputFormat

Output a single integer to standard output (stdout): the minimum number of moves required to reach the bottom-right corner from the top-left corner. If the destination cannot be reached, output -1.

## sample
4 4
....
.##.
....
....
6