using static System.Math;using static System.Console;using System.Collections.Generic;using System.Linq;using System;using System.Collections;


class Program
{
    #region Reader
    static string ReadStr => Console.ReadLine();
    static string[] ReadStrs => Console.ReadLine().Split(' ');
    static int ReadInt => Convert.ToInt32(Console.ReadLine().Trim());
    static int[] ReadInts => Console.ReadLine().Trim().Split(' ').Select(s => Convert.ToInt32(s)).ToArray();
    static long ReadLong => Convert.ToInt64(Console.ReadLine().Trim());
    static long[] ReadLongs => Console.ReadLine().Trim().Split(' ').Select(s => Convert.ToInt64(s)).ToArray();
    #endregion
    #region Method
    const int mod = 1000000007;
    public static int Mod(int a,int mod) { return a % mod >= 0 ? a % mod : a % mod + mod; }
    public static long Mod(long a, long mod = mod) { return a % mod >= 0 ? a % mod : a % mod + mod; }
    #endregion


    static void Main()
    {
        List<int> anses = new List<int>();
        while (true)
        {
            int[] WH = ReadInts;
            int H = WH[1];
            int W = WH[0];
            if (H == 0 && W == 0)
            {
                break;
            }

            List<int[]>[,] walls = new List<int[]>[H, W];

            for (int h = 0; h < H; h++)
            {
                for (int w = 0; w < W; w++)
                {
                    walls[h, w] = new List<int[]>();
                }
            }

            for (int i = 0; i < H - 1; i++)
            {
                int[] v_wall = ReadInts;
                for (int v_w = 0; v_w < v_wall.Length; v_w++)
                {
                    if (v_wall[v_w] == 1)
                    {
                        walls[i, v_w].Add(new int[2] { i, v_w + 1 });
                        walls[i, v_w + 1].Add(new int[2] { i, v_w });

                    }
                }

                int[] h_wall = ReadInts;
                for (int h_w = 0; h_w < h_wall.Length; h_w++)
                {
                    if (h_wall[h_w] == 1)
                    {
                        walls[i + 1, h_w].Add(new int[2] { i, h_w });
                        walls[i, h_w].Add(new int[2] { i + 1, h_w });

                    }
                }
            }

            int[] last_v_wall = ReadInts;
            for (int v_w = 0; v_w < last_v_wall.Length; v_w++)
            {
                if (last_v_wall[v_w] == 1)
                {
                    walls[H - 1, v_w].Add(new int[2] { H - 1, v_w + 1 });
                    walls[H - 1, v_w + 1].Add(new int[2] { H - 1, v_w });

                }
            }

            int[] start = { 0, 0, 0 };
            int[] goal = { H - 1, W - 1, 0 };

            Queue<int[]> queue = new Queue<int[]>();
            queue.Enqueue(start);
            bool[,] done = new bool[H, W];
            done[0, 0] = true;

            int[][] dirs = new int[4][] { new int[2] { 1, 0 }, new int[2] { 0, 1 }, new int[2] { -1, 0 }, new int[2] { 0, -1 } };

            int ans = 0;
            while (queue.Count > 0)
            {
                int[] current = queue.Dequeue();

                foreach (var dir in dirs)
                {
                    int[] next = new int[3] { current[0] + dir[0], current[1] + dir[1], current[2] + 1 };
                    if (next[0] < 0 || next[0] >= H || next[1] < 0 || next[1] >= W)
                    {
                        continue;
                    }

                    if (!walls[current[0], current[1]].Any(x => x[0] == next[0] && x[1] == next[1]))
                    {
                        if (done[next[0], next[1]])
                        {
                            continue;
                        }

                        done[next[0], next[1]] = true;
                        queue.Enqueue(next);
                        if (next[0] == goal[0] && next[1] == goal[1])
                        {
                            ans = next[2];
                            break;
                        }
                    }

                }


            }


            anses.Add(ans != 0 ? ans + 1 : 0);
        }
        WriteLine(string.Join("\n", anses));
    }

  



}



Measured memory_bytes: 22676