/*
	?§£????????????:How Many Islands?
	(http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp)

	Status:Accepted

	??????:
		???????????????????????£??????????????????????????£????????????????????¨??????????????¨???????????°??????????????????????????¨???????????¨?????§?????????
        ?????????????????§????????????????§£??????????????????????????????????????±???????????°???????????°????????¨?????????????????????????????????????????????????????????

*/
var log = console.log;
if (typeof process != "undefined") {
    var input = "";
    process.stdin.resume();
    process.stdin.setEncoding('utf8');
    process.stdin.on('data', function(chunk) {
        input += chunk;
    });
    process.stdin.on('end', function() {
        var lines = input.split("\n");
        main(lines);
    });
}


function main(lines) {
    lines = lines.map(function(line) {
        return line.split(" ").filter(function(e) {
            return e != "";
        }).map(function(e) {
            return Number(e);
        });
    });
    while (lines[0][0] != 0 && lines[0][1] != 0) {
        var fl = lines.shift();
        var W = fl[0];
        var H = fl[1];
        var set = lines.slice(0, 2 * H - 1);
        lines = lines.slice(2 * H - 1);
        processSet(W, H, set);
    }
}

function processSet(W, H, set) {
    //log(set);

    function is_able_to(pos1, pos2) {
        if (pos1[0] != pos2[0]) {
            //????§????
            //?\???°index???????´?????????????..1,3,5..
            var walls = set[2 * Math.min(pos1[0], pos2[0]) + 1];
            var inq = walls[pos1[1]];
            return Boolean(inq != 1); //1??§????????£??????ok
        } else if (pos1[0] == pos2[0]) {
            //?¨??§????
            //??¶??°index???????´?????????????..0,2,4..
            //?£??????????????????\??£????????¨???
            var walls = set[2 * pos1[0]];
            //??¬?????????????????¨???
            var inq = walls[Math.min(pos1[1], pos2[1])];
            return Boolean(inq != 1); //1??§????????£??????ok
        }
        return false;
    }
    var mins = [];
    for (var h = 0; h < H; h++) {
        mins[h] = [];
        for (var w = 0; w < W; w++) {
            mins[h][w] = Infinity;
        }
    }
    mins[0][0] = 1;
    var stack = [];
    stack.push([0, 0]);
    var ngs = [
        [-1, 0],
        [0, -1],
        [0, 1],
        [1, 0]
    ];
    while (stack.length > 0) {
        var now = stack.shift();
        var ns = mins[now[0]][now[1]];
        //log("now at " + now);
        ngs.forEach(function(d) {
            var pos2 = [];
            pos2[0] = now[0] + d[0];
            pos2[1] = now[1] + d[1];
            if (
                0 <= pos2[0] && pos2[0] < H &&
                0 <= pos2[1] && pos2[1] < W
            ) {
                if (is_able_to(now, pos2)) {
                    if (mins[pos2[0]][pos2[1]] == Infinity) {
                        //not visited
                        mins[pos2[0]][pos2[1]] = ns + 1;
                        stack.push(pos2);
                    }
                }
            }
        });
    }
    if (mins[H - 1][W - 1] == Infinity) {
        log(0);
    } else {
        log(mins[H - 1][W - 1]);
    }
}

Measured memory_bytes: 27284