H, W = map(int, input().split()) A = [input() for _ in range(H)] l = [[0] * W for i in range(H)] l[0][0]=1 l[H-1][W-1]=1 aa=0 ab=0 ba=H-1 bb=W-1 a=0 b=0 c=1 cnt=2 while (c>=1): m=0 if(a%2==0): if(aa>0 and l[aa-1][ab]==0): if(m==0 or A[aa-1][ab]0 and l[aa][ab-1]==0): if(m==0 or A[aa][ab-1]0): if(l[a-1][b]==2): break if(b>0): if(l[a][b-1]==2): break if(a0 and l[ba-1][bb]==0): if(m==0 or A[ba-1][bb]0 and l[ba][bb-1]==0): if(m==0 or A[ba][bb-1]W and l[ba+1][bb]==0): if(m==0 or A[ba+1][bb]H and l[ba][bb+1]==0): if(m==0 or A[ba][bb+1]0): if(l[a-1][b]==1): break if(b>0): if(l[a][b-1]==1): break if(a