#K5286. Shortest Path in a Maze

    ID: 29403 Type: Default 1000ms 256MiB

Shortest Path in a Maze

Shortest Path in a Maze

In this problem, you are given a maze represented as a grid with (N) rows and (M) columns. Each cell in the maze is either open (denoted by '.') or blocked (denoted by '#'). Your task is to determine the length of the shortest path from the top-left corner ((0, 0)) to the bottom-right corner ((N-1, M-1)) using only the four primary directions: up, down, left, and right.

If no valid path exists or if the starting or ending point is blocked, you should print Impossible.

inputFormat

The input is read from standard input (stdin). The first line contains two integers (N) and (M) separated by a space, representing the number of rows and columns in the maze respectively. The following (N) lines each contain a string of length (M), where each character is either '.' (an open cell) or '#' (a blocked cell).

outputFormat

Output the length of the shortest path from the top-left corner to the bottom-right corner on a single line. If there is no such path, output Impossible.## sample

4 5
.....
.#...
....#
.....
8