using System;
using System.Collections.Generic;
using System.Linq;

namespace AOJVolume11_1166
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> anss = new List<int>();
            while (true)
            {
                var wh = ReadInt();
                w = wh[0];
                h = wh[1];
                if (w == 0 && h == 0) break;

                horizonalWalls = new int[h + 1, w];
                verticalWalls = new int[h, w + 1];
                for (int i = 0; i < 2 * h - 1; i++)
                {
                    if (i % 2 == 1) //horizon
                    {
                        var input = ReadInt();
                        for (int j = 0; j < w; j++)
                        {
                            horizonalWalls[i / 2 + 1, j] = input[j];
                        }
                    }
                    else //vert
                    {
                        Console.Read();
                        var input = ReadInt();
                        for (int j = 0; j < w - 1; j++)
                        {
                            verticalWalls[i / 2, j + 1] = input[j];
                        }
                    }
                }

                map = new int[h, w];
                int[,] d = new int[h, w];
                Queue<int> queX = new Queue<int>();
                Queue<int> queY = new Queue<int>();
                queX.Enqueue(0);
                queY.Enqueue(0);
                d[0, 0] = 1;


                while (queX.Count > 0)
                {
                    int x = queX.Dequeue();
                    int y = queY.Dequeue();

                    if (map[y, x] == 1) continue;
                    map[y, x] = 1;

                    for (int i = 0; i < 4; i++)
                    {
                        if (CanGo(x, y, x + dx[i], y + dy[i]))
                        {
                            queX.Enqueue(x + dx[i]);
                            queY.Enqueue(y + dy[i]);
                            d[y + dy[i], x + dx[i]] = d[y, x] + 1;
                        }
                    }
                }

                anss.Add(d[h - 1, w - 1]);
            }

            foreach (var item in anss)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }

        static int[] dx = new int[] { 0, 1, 0, -1 };
        static int[] dy = new int[] { -1, 0, 1, 0 };
        static int[,] horizonalWalls;
        static int[,] verticalWalls;
        static int[,] map;
        static int w;
        static int h;
        static bool CanGo(int fromx, int fromy, int tox, int toy)
        {
            //マイナスか範囲外ならfalse
            if (tox < 0 || tox >= w) return false;
            if (toy < 0 || toy >= h) return false;

            //水平壁が1あったらfalse
            if (fromy < toy && horizonalWalls[toy, fromx] == 1) return false;
            else if (fromy > toy && horizonalWalls[toy + 1, fromx] == 1) return false;

            //垂直壁が1あったらfalse
            if (fromx < tox && verticalWalls[toy, tox] == 1) return false;
            if (fromx > tox && verticalWalls[toy, tox + 1] == 1) return false;

            if (map[toy, tox] == 1) return false;

            return true;
        }

        static int[] ReadInt()
        {
            int[] ret = Console.ReadLine().Split().Select(int.Parse).ToArray();
            return ret;
        }
        static long[] ReadLong()
        {
            long[] ret = Console.ReadLine().Split().Select(long.Parse).ToArray();
            return ret;
        }
    }
}



Measured memory_bytes: 20912