cho.sh
Notes
Loading...

Maze Search

Time limit

1s

Memory limit

192 MB

Problem

You are given a maze with N rows and M columns. Each cell contains either 1 or 0. A cell with 1 can be entered, and a cell with 0 cannot be entered.

Starting from position (1, 1), find the minimum number of cells that must be passed through to reach position (N, M). You may move only to an adjacent cell sharing an edge. The starting cell and the destination cell are both included in the count.

Input

The first line contains two integers N and M (2 <= N, M <= 100). Each of the next N lines contains M digits describing the maze. The digits are given without spaces.

Output

Print the minimum number of cells that must be passed through. The input is guaranteed to contain at least one path to the destination.