include[HTML_REMOVED]
using namespace std;
const int N = 110;
int n, m;
int g[N][N];
//int d[N][N];
int tx, ty;
struct elem{
int x;
int y;
int s;
};
int nextt[4][2] = { {0, 1},//right
{1, 0},//down
{0, -1},//left
{-1, 0}};//up
struct elem q[N * N];
int book[N][N];
int flag = 0;
int bfs(){
int hh = 1;
int tt = 1;
//d[0][0] = 0;
q[tt] = {0,0,0};
book[0][0] = 1;
tt++;
while(hh < tt){
for(int k = 0;k <= 3; k++){
tx = q[hh]. x + nextt[k][0];
ty = q[hh]. y + nextt[k][1];
if(tx >= 0 && ty >= 0 && tx < n && ty < m && g[tx][ty] == 0 && book[tx][ty] == 0){
q[tt].x = tx;
q[tt].y = ty;
q[tt].s = q[hh].s + 1;
book[tx][ty] = 1;
tt++;
}
if(tx == n - 1 && ty == m - 1){
flag = 1;
break;
}
}
if(flag == 1)
break;
hh++;
}
return q[tt - 1].s;
}
int main(){
cin >> n >> m;
for(int i = 0;i < n; i++)
for(int j = 0; j < m; j++)
cin >> g[i][j];
cout << bfs() << endl;
return 0;
}