Path on binary matrix - MarisaOJ: Marisa Online Judge

Path on binary matrix

Time limit: 1000 ms
Memory limit: 256 MB
You are given an binary matrix $A$ of $n$ rows and $m$ columns. In a move, from one cell, you can go to any of 4 adjacent cells (i.e. share the same edge). You cannot move to cell with number $1$. Find the shortest distance from $(1, 1)$ to $(n, m)$. ### Input - The first line contains 2 integers $n, m$. - The next $n$ lines, each line contains a binary string of length $m$, matrix $A$. ### Output - Print the distance from $(1, 1)$ to $(n, m)$, or print `-1` if no path exists. ### Constraints - $1 \le n, m \le 10^3$. ### Example Input: ``` 4 5 01000 01010 00010 11010 ``` Output: ``` 11 ```