#K55782. Minimum Steps in a Maze

    ID: 30052 Type: Default 1000ms 256MiB

Minimum Steps in a Maze

Minimum Steps in a Maze

You are given a maze represented by a grid of dimensions (N \times M). Each cell in the maze contains either a '0' or a '1', where '0' denotes a passable cell and '1' denotes a wall. You start at the top-left cell (0,0) and need to reach the bottom-right cell ((N-1, M-1)). Your task is to determine the minimum number of steps required to reach the destination. Movement is allowed in the four cardinal directions: up, down, left, and right. If there is no path from the start to the destination, output -1.


Note: The coordinates of the start cell are ( (0,0) ) and the destination cell is ( (N-1, M-1) ).

inputFormat

Input is read from standard input (stdin).

The first line contains two space-separated integers, (N) and (M), representing the number of rows and columns in the maze respectively. Following that, there are (N) lines, each containing a string of length (M). Each character in the string is either '0' (open path) or '1' (wall).

outputFormat

Output a single integer to standard output (stdout): the minimum number of steps required to reach the bottom-right cell. If it is not possible to reach the destination, output -1.## sample

5 5
00000
01110
00010
01110
00000
8