
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e3+5;
ll mp[maxn][maxn],vis[maxn][maxn],way[maxn][maxn];
ll delta[4][2]={
{
1,0},{
0,-1},{
0,1},{
-1,0}};
char ans[maxn];
char cc[5]="DLRU";
ll n,m;
struct node{
ll x,y;
};
int main()
{
cin>>n>>m;
for(ll i=1;i<=n;i++){
for(ll j=1;j<=m;j++){
char x;cin>>x;
mp[i][j]=x-'0';
}
}
queue<node> q;
node tmp={
1,1};
q.push(tmp);vis[1][1]=1;
while(!q.empty()){
node now=q.front();
q.pop();
for(ll k=0;k<4;k++){
ll x=now.x+delta[k][0