#K57102. Minimum Moves to Reach End

    ID: 30346 Type: Default 1000ms 256MiB

Minimum Moves to Reach End

Minimum Moves to Reach End

You are given a grid with n rows and m columns. Each cell in the grid is either an open cell represented by '.' or an obstacle represented by '#'. Starting from the top-left corner (0,0), your task is to determine the minimum number of moves required to reach the bottom-right corner (n-1, m-1). You can only move in four directions: up, down, left, and right.

A move is defined as one step in one of the following directions:

$$\{(0, 1), (1, 0), (0, -1), (-1, 0)\}$$

If it is impossible to reach the destination, output -1.

inputFormat

The input is given via standard input and has the following format:

n m
s1
s2
...
sn

Where:

  • n and m are two integers representing the number of rows and columns respectively.
  • Each si is a string of length m consisting of the characters '.' and '#' representing the grid.

outputFormat

Output a single integer to standard output: the minimum number of moves required to reach the bottom-right corner from the top-left corner. If it is impossible, output -1.

## sample
3 3
..#
.#.
...
4