//
//  main.c
//  Amazing Mazes
//
//  Created by x15071xx on 2016/06/18.
//  Copyright ?? 2016??´ AIT. All rights reserved.
//

#include <stdio.h>
#define INFN (1000000);
int main(int argc, const char * argv[]) {
    
    while (1) {
        int w,h,field[65][65],i,j,count;
        
        for (i=0; i<65; i++) {
            for (j=0; j<65; j++) {
                field[i][j] = 0;
            }
        }
        
        scanf("%d %d",&w,&h);
        field[h*2-2][w*2-2] = 2;
        if (w == 0 && h == 0) {
            break;
        }
        
        
        for (i=0; i< h-1; i++) {
            
            scanf(" %d",&field[i*2][1]);
            //?????????
            for (j=1; j<w-1; j++) {
                scanf("%d",&field[i*2][j*2+1]);
            }
            //?¨???????
            for (j=0; j<w; j++) {
                scanf("%d",&field[i*2+1][j*2]);
            }
        }
        //?????????
        scanf(" %d",&field[i*2][1]);
        
        for (j=1; j<w-1; j++) {
            scanf("%d",&field[i*2][j*2+1]);
        }

//        printf("\n");
//        for (i=0; i<h*2-1; i++) {
//            for (j=0; j<w*2-1; j++) {
//                printf("%d ",field[i][j]);
//            }
//            printf("\n");
//        }
        
        //??¢?´¢????§?
        int d[65][65];
        int dx[4] ={1,0,-1,0}, dy[4] = {0,1,0,-1};
        int sx=0,sy=0,gx= w*2-2,gy=h*2-2;
        int queue[360][2],tmp=0;
        int queue_count=0,queue_front=0;
        int pair[2] = {0,0};
        int x = 0,y = 0;
        for (i=0; i<h*2-1; i++) {
            for (j=0; j<w*2-1; j++) {
                d[i][j] = INFN;
            }
        }
        
        queue[queue_count][0] = sy;
        queue[queue_count][1] = sx;
        queue_count++;
        d[sy][sx]=0;
        
        while (queue_count) {
            //?????\???????????????????????????
            pair[0] = queue[queue_front][0]; pair[1] = queue[queue_front][1];
            queue_count--;
            if (queue_count != 0) {
                for (i=0; i<360-1; i++) {
                    tmp=queue[i][0];
                    queue[i][0] = queue[i+1][0];
                    queue[i+1][0] = tmp;
                    tmp=queue[i][1];
                    queue[i][1] = queue[i+1][1];
                    queue[i+1][1] = tmp;
                }
            }
            
            //???????????????????????¶????????´??????????????????????????????
            if (pair[0] == gy && pair[1] == gx) {
                break;
            }
            
            for (i=0; i<4; i++) {
                int nx = pair[1] + dx[i];
                int ny = pair[0] + dy[i];
                if (i == 0) {
                    x=1;
                    y=0;
                } else if (i == 1){
                    y=1;
                    x=0;
                } else if (i == 2){
                    x=-1;
                    y=0;
                } else if (i == 3){
                    y=-1;
                    x=0;
                }
                
                if ((0<=nx)&&(nx<2*w-1)&&(0<=ny)&&(ny<2*h-1)&&(field[ny][nx]!=1)&&(d[ny+y][nx+x]==1000000)) {
                    queue[queue_count][0] =ny+y;queue[queue_count][1] =nx+x;
                    queue_count++;
//                    if (i == 0) {
//                        x=2;
//                        y=0;
//                    } else if (i == 1){
//                        y=2;
//                        x=0;
//                    } else if (i == 2){
//                        x=-2;
//                        y=0;
//                    } else if (i == 3){
//                        y=-2;
//                        x=0;
//                    }
//
                    d[ny+y][nx+x] = d[pair[0]][pair[1]]+1;
                }
            }
        }
        
        if (d[gy][gx]==1000000) {
            printf("0\n");
        } else {
            printf("%d\n",d[gy][gx]+1);
        }
        
    }
    
    return 0;
}