#include<iostream>#include<queue>#include<algorithm>#include<cstring>usingnamespace std;int ch[111][111];int op[111][111];int f1[]={0,0,1,-1}, f2[]={1,-1,0,0};structnode{int x, y;};intmain(){queue<node> cun;memset(op,-1,sizeof op);memset(ch,1,sizeof ch);int n, m;cin >> n >> m;for(int i =1; i <= n; i++){for(int j =1; j <= m; j++){cin >> ch[i][j];}}cun.push({1,1});op[1][1]=0;while(cun.size()){int x = cun.front().x;int y = cun.front().y;cun.pop();for(int i =0; i <4; i++){int xx = x + f1[i];int yy = y + f2[i];if(xx >0&& yy >0&& xx <= n && yy <= m)if(ch[xx][yy]==0&& op[xx][yy]==-1){op[xx][yy]= op[x][y]+1;cun.push({xx, yy});}}}cout<<op[n][m];return0;}