#K65952. Taco Grid Shortest Path

    ID: 32311 Type: Default 1000ms 256MiB

Taco Grid Shortest Path

Taco Grid Shortest Path

You are given a grid of cells represented as E (empty) and F (forbidden). Your task is to find the length of the shortest path from the top-left corner to the bottom-right corner of the grid, moving only in the four cardinal directions (up, down, left, right). The path must not pass through any cell marked with F. If no such path exists, output -1.

Note: The grid is provided via standard input. The first line of input contains two integers, H and W, representing the number of rows and columns respectively. This is followed by H lines, each containing a string of length W composed only of the characters E and F.

The path length is defined as the number of moves required to reach the destination (starting position is counted as 0 moves). If the starting cell or the destination cell is forbidden (F), then no valid path exists.

inputFormat

The input is read from standard input. The first line contains two space-separated integers H and W (the number of rows and columns respectively). Each of the following H lines contains a string of length W consisting only of the characters E and F.

Example:

5 5
EEEEF
EFFFF
EEEEE
EFEEE
EEEFE

outputFormat

Output a single integer to standard output – the length of the shortest path from the top-left to the bottom-right cell. If no such path exists, output -1.

## sample
5 5
EEEEF
EFFFF
EEEEE
EFEEE
EEEFE
8