from collections import deque

def gset(c,r,w,f,g):
    for ri in range(r):
        for ci in range(c):
#            if ci>0 and ri>0 and w[ri][ci-1]==0:
            if ci>0 and w[ri][ci-1]==0:
                g[ri][ci].append((ri,ci-1))
#            if ci<c-1 and ri<r-1 and w[ri][ci]==0:
            if ci<c-1 and w[ri][ci]==0:
                g[ri][ci].append((ri,ci+1))
            if ri>0 and f[ri-1][ci]==0:
                g[ri][ci].append((ri-1,ci))
            if ri<r-1 and f[ri][ci]==0:
                g[ri][ci].append((ri+1,ci))   

def bfs(g):
    q=deque()
    q.append((0,0))

    parent=[[-1]*(c) for i in range(r)]
    parent[0][0]=0
    yn=""
    while q:
#        print(q)
        sty,stx = q.popleft()
        for vy,vx in g[sty][stx]:
            if r-1==vy and c-1==vx:
                dist[vy][vx]=dist[sty][stx]+1
                yn="yes"
                break
            elif parent[vy][vx] !=-1:
                continue
            else:
                parent[vy][vx]=(sty,stx)
                q.append((vy,vx))
                dist[vy][vx]=dist[sty][stx]+1
        if yn=="yes":
            break
#    print(parent)
    if yn=="yes":
        print(dist[r-1][c-1]+1)
    else:
        print(0)


icnt=0
icase=0
while True:
    c,r=map(int,input().split())
    if c==0 and r==0:
        break
    else:
        w=[[0] *(c-1) for i in range(r)]
        f=[[0] *c for i in range(r-1)]
        dist=[[0] *c for i in range(r)]
        if icase==0:
            for i in range(r-1):
                w[i]=list(map(int,input().split()))
                f[i]=list(map(int,input().split()))
            w[r-1]=list(map(int,input().split()))
        elif icase==1:
            f= [[0, 1], [1, 0]]
            w= [[1], [0], [1]]
        elif icase==2:
#            26 29
            w[0]=" 1 1 0 1 0 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1"
            f[0]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[1]=" 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1"
            f[1]="0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[2]=" 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1"
            f[2]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[3]=" 1 1 1 0 1 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 0 0 1 0 1"
            f[3]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[4]=" 1 1 1 1 0 1 1 0 1 1 1 1 0 1 1 0 1 1 1 1 1 1 0 1 1"
            f[4]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[5]=" 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1"
            f[5]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[6]=" 0 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 0 1 1 0 1"
            f[6]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1"
            w[7]=" 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1"
            f[7]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[8]=" 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1 1 0 0 1 1 1 0 1"
            f[8]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1"
            w[9]=" 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1"
            f[9]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0"
            w[10]=" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1"
            f[10]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[11]=" 1 1 1 1 1 1 0 1 1 0 1 0 0 1 1 1 1 0 0 1 1 1 1 0 1"
            f[11]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[12]=" 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1"
            f[12]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[13]=" 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1"
            f[13]="0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[14]=" 1 1 1 0 1 1 0 1 0 0 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1"
            f[14]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[15]=" 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0"
            f[15]="0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[16]=" 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 0 1 0 1"
            f[16]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[17]=" 1 1 0 1 0 0 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1"
            f[17]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[18]=" 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1"
            f[18]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[19]=" 1 1 1 1 1 1 1 1 0 1 0 0 0 1 1 1 0 1 1 1 1 1 1 1 1"
            f[19]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[20]=" 1 1 1 1 1 1 0 1 0 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1"
            f[20]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[21]=" 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1"
            f[21]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[22]=" 0 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1"
            f[22]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[23]=" 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 0 1 1 0"
            f[23]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[24]=" 1 1 1 1 1 0 1 1 1 1 1 0 0 0 1 1 1 1 1 0 1 1 1 1 1"
            f[24]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[25]=" 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1"
            f[25]="0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0"
            w[26]=" 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0"
            f[26]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[27]=" 1 0 1 1 1 1 1 1 1 0 1 1 0 0 1 1 1 1 1 1 1 1 1 0 1"
            f[27]="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0"
            w[28]=" 1 1 0 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1"
            for i in range(r-1):
                w[i]=list(map(int,w[i].split()))
                f[i]=list(map(int,f[i].split()))
            w[r-1]=list(map(int,w[r-1].split()))
        elif icase==3:
            ff=open("D:\999atcoder\q100\q032\q032in.txt")
            ll=ff.readline()
            for i in range(r-1):
                ll=ff.readline()
                w[i]=list(map(int,ll.split()))
                ll=ff.readline()
                f[i]=list(map(int,ll.split()))
            ll=ff.readline()
            w[r-1]=list(map(int,ll.split()))
            ff.close()
            icnt+=1
            if icnt>1:
                break
        g=[[[] for j in range(c)] for i in range(r)]
        gset(c,r,w,f,g)
    
#        print(g)
        
        bfs(g)



Measured memory_bytes: 6228