#C6266. Minimum Moves on a Grid

    ID: 50007 Type: Default 1000ms 256MiB

Minimum Moves on a Grid

Minimum Moves on a Grid

You are given a grid with n rows and m columns. Each cell of the grid is either 0 or 1, where 0 indicates an empty cell and 1 indicates an obstacle. A robot starts at the top-left cell (0, 0) and wants to reach the bottom-right cell (n-1, m-1). The robot can move up, down, left, or right at each step and cannot move through cells with obstacles. Your task is to compute the minimum number of moves required for the robot to reach the destination. If it is impossible to reach the destination, output -1.

The problem can be formulated in a graph search context where each cell is a node and valid moves are edges. A breadth-first search (BFS) from the starting cell can be used to find the shortest path in terms of moves.

Note: The input will be provided through standard input (stdin) and the output should be printed to standard output (stdout).

inputFormat

The first line contains two integers n and m, which represent the number of rows and columns in the grid respectively.

The following n lines each contain m space-separated integers (either 0 or 1) representing the grid cells.

outputFormat

Output a single integer which is the minimum number of moves required for the robot to reach the bottom-right cell. If it is impossible to reach, output -1.

## sample
3 3
0 0 0
0 1 0
0 0 0
4