#C6890. Taco Grid Shortest Path

    ID: 50700 Type: Default 1000ms 256MiB

Taco Grid Shortest Path

Taco Grid Shortest Path

You are given an n x m grid where each cell is either open (denoted by .) or blocked (denoted by #). Your task is to find the shortest path from the top-left cell at coordinates \( (0, 0) \) to the bottom-right cell at coordinates \( (n-1, m-1) \), moving only in the four cardinal directions (up, down, left, right). You cannot move into cells with obstacles.

If a valid path exists, output the length of the shortest path (i.e. the number of moves needed). If no such path exists, output \( -1 \).

Note: The starting cell and the destination cell must be open (i.e. not blocked).

inputFormat

The input is given via standard input with the following format:

  1. The first line contains two space-separated integers \( n \) and \( m \), representing the number of rows and columns of the grid respectively.
  2. The next \( n \) lines each contain a string of length \( m \) consisting only of the characters . (open cell) and # (obstacle), representing the grid.

outputFormat

Output a single integer on standard output: the length of the shortest path from the top-left corner to the bottom-right corner. If no such path exists, output \( -1 \).

## sample
5 5
.....
.###.
...#.
.###.
.....
8