13204 - The Only Way Back Home   

Description

Input

First line, you will get the row and column of the matrix.

1 <= R <= 105, 1 <= C <= 105

Second line, you will get a 2D matrix, for each element N in the matrix:

0 <= N <= 1

Output

If there is no path to the destination, return 0.

If there is a path to the destination, return the distance of the path from root to destination. It is guaranteed that there is only one solution for each testcase.

(Remember to print out "\n" after the answer)

Sample Input  Download

Sample Output  Download

Tags




Discuss