#K50357. Minimum Moves in a Forest

    ID: 28846 Type: Default 1000ms 256MiB

Minimum Moves in a Forest

Minimum Moves in a Forest

You are given a forest represented as an \(N \times M\) grid. Each cell in the grid is either open (represented by '.') or blocked by an obstacle (represented by '#'). A bird starts from a given cell \((S_{row}, S_{col})\) and needs to reach the destination cell \((D_{row}, D_{col})\). The bird can move one step at a time in four directions: up, down, left, or right.

Your task is to determine the minimum number of moves required for the bird to reach its destination. If the destination cannot be reached, output \(-1\).

Note: The moves count is the number of steps taken from the start cell to the destination cell using the shortest possible path.

inputFormat

The input is read from standard input (stdin) in the following format:

N M
row_1
row_2
... (N rows in total representing the forest grid)
S_row S_col
D_row D_col

Here, the first line contains two integers \(N\) and \(M\) which are the number of rows and columns in the grid, respectively. The next \(N\) lines each contain a string of length \(M\) representing a row of the forest. Then, the starting cell coordinates \(S_{row}\) and \(S_{col}\) are provided, followed by the destination cell coordinates \(D_{row}\) and \(D_{col}\). All indices are 0-indexed.

outputFormat

The output is a single integer written to standard output (stdout) - the minimum number of moves required for the bird to reach the destination. If the destination cannot be reached, output \(-1\).

## sample
5 5
..#..
.#.#.
.#.#.
.#...
.....
0 0
4 4
8