#C9051. Minimum Steps in a Grid

    ID: 53102 Type: Default 1000ms 256MiB

Minimum Steps in a Grid

Minimum Steps in a Grid

You are given a rectangular grid of dimensions \(R \times C\) consisting of characters '0' and '1'. A cell with '0' denotes a free cell, whereas '1' represents an obstacle. Starting from the top-left cell (0, 0), your goal is to reach the bottom-right cell (R-1, C-1) by moving only to the right or down.

Find the minimum number of steps required to travel from the start to the destination. If either the starting or ending cell is blocked or if there is no valid path under the given movement constraints, output \(-1\).

Note: At each step, you can only move either one cell to the right or one cell down. The grid is provided as \(R\) rows of binary strings, each of length \(C\).

inputFormat

The first line of input contains two space-separated integers \(R\) and \(C\), representing the number of rows and columns in the grid, respectively. Each of the following \(R\) lines contains a binary string of length \(C\), where '0' indicates an open cell and '1' indicates an obstacle.

Example:

5 5
00000
01110
00010
01110
00000

outputFormat

Output a single integer on a new line, representing the minimum number of steps required to reach from the top-left to the bottom-right corner using only right and down moves. If no valid path exists, output \(-1\).

## sample
5 5
00000
01110
00010
01110
00000
8

</p>