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

namespace Aizu
{
    class Program
    {
        static void Main(string[] args)
        {
            var answers = new List<int>();

            while (true)
            {
                var firstLine = Console.ReadLine().Split(' ').Select(value => int.Parse(value)).ToArray();
                var w = firstLine[0];
                var h = firstLine[1];

                if(w == 0 && h == 0)
                {
                    break;
                }

                var horizontalWalls = new List<bool[]>();
                var verticalWalls = new List<bool[]>();
                for (var i = 0; i < h; i++)
                {
                    verticalWalls.Add(Console.ReadLine().Split(' ').Select(value => value == "1").Skip(1).ToArray());
                    if (i == h - 1)
                    {
                        break;
                    }
                    horizontalWalls.Add(Console.ReadLine().Split(' ').Select(value => value == "1").ToArray());
                }

                answers.Add(Bfs(verticalWalls, horizontalWalls));
            }

            answers.ForEach(value => Console.WriteLine(value));
        }

        private static int Bfs(List<bool[]> verticalWalls, List<bool[]> horizontalWalls)
        {
            var costs = new int[verticalWalls.Count, horizontalWalls[0].Length];
            var isFound = new bool[verticalWalls.Count, horizontalWalls[0].Length];
            var queue = new Queue<int[]>();
            queue.Enqueue(new int[2] { 0, 0 });
            costs[0, 0] = 1;
            var canGoal = false;

            while(queue.Count > 0)
            {
                var position = queue.Dequeue();
                var y = position[0];
                var x = position[1];

                isFound[y, x] = true;

                var destY = y + 1;
                var destX = x;
                if(destY < verticalWalls.Count && !isFound[destY, destX] && !horizontalWalls[destY - 1][destX])
                {
                    isFound[destY, destX] = true;
                    costs[destY, destX] = costs[y, x] + 1;
                    if(destY == verticalWalls.Count - 1 && destX == horizontalWalls[0].Length - 1)
                    {
                        canGoal = true;
                        break;
                    }
                    queue.Enqueue(new int[2] { destY, destX });
                }
                destY = y - 1;
                destX = x;
                if (destY >= 0 && !isFound[destY, destX] && !horizontalWalls[destY][destX])
                {
                    isFound[destY, destX] = true;
                    costs[destY, destX] = costs[y, x] + 1;
                    queue.Enqueue(new int[2] { destY, destX });
                }
                destY = y;
                destX = x + 1;
                if (destX < horizontalWalls[0].Length && !isFound[destY, destX] && !verticalWalls[destY][destX - 1])
                {
                    isFound[destY, destX] = true;
                    costs[destY, destX] = costs[y, x] + 1;
                    if (destY == verticalWalls.Count - 1 && destX == horizontalWalls[0].Length - 1)
                    {
                        canGoal = true;
                        break;
                    }
                    queue.Enqueue(new int[2] { destY, destX });
                }
                destY = y;
                destX = x - 1;
                if (destX >= 0 && !isFound[destY, destX] && !verticalWalls[destY][destX])
                {
                    isFound[destY, destX] = true;
                    costs[destY, destX] = costs[y, x] + 1;
                    queue.Enqueue(new int[2] { destY, destX });
                }
            }

            return canGoal ? costs[verticalWalls.Count - 1, horizontalWalls[0].Length - 1] : 0;
        }
    }
}



Measured memory_bytes: 20868