#K47492. Minimum Moves to Reach End

    ID: 28210 Type: Default 1000ms 256MiB

Minimum Moves to Reach End

Minimum Moves to Reach End

You are given a grid of dimensions \(n \times m\) consisting of characters '.' (open cell) and '#' (obstacle). A robot starts at the top-left corner (cell \((1,1)\)) and needs to reach the bottom-right corner (cell \((n,m)\)). The robot can move one step at a time in the four cardinal directions: up, down, left, and right.

Your task is to determine the minimum number of moves required to reach the destination. If the destination is unreachable, output \(-1\).

Note: The robot cannot move outside the grid or enter a cell with an obstacle. The positions are 1-indexed.

inputFormat

The first line contains two integers \(n\) and \(m\) representing the number of rows and columns of the grid.

The following \(n\) lines each contain a string of length \(m\), representing a row of the grid. Each character is either '.' or '#'.

outputFormat

Output a single integer: the minimum number of moves required for the robot to reach the bottom-right corner, or \(-1\) if it is impossible.

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