using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    internal class Program
    {
        static void Main()
        {
            string[] s = Console.ReadLine().Split(' ');
            long n = long.Parse(s[0]);
            long m = long.Parse(s[1]);
            long[,] matrica = new long[n, m];
            long suma = 0, suma2 = 0;
            for (long i = 0; i < n; i++)
            {
                s = Console.ReadLine().Split(' ');
                for (long j = 0; j < m; j++)
                {
                    matrica[i, j] = long.Parse(s[j]);
                }
            }
            if (n == 1 && m == 1)
            {
                suma = 0;
            }
            else if (n == 1)
            {
                suma = matrica[0, 1] ^ matrica[0, 0];
                suma2 = matrica[0, 0] ^ matrica[0, 1];
                if (suma2 < suma)
                {
                    suma = suma2;
                }
                //Console.WriteLine("drugi {0} {1}", suma, suma2);
            }
            else if (m == 1)
            {
                suma = matrica[1, 0] ^ matrica[0, 0];
                suma2 = matrica[0, 0] ^ matrica[1, 0];
                if (suma2 < suma)
                {
                    suma = suma2;
                }
                //Console.WriteLine("treci {0} {1}", suma, suma2);
            }
            else
            {
                suma = (matrica[0, 0] + matrica[0, 1] + matrica[1, 1] + matrica[1, 0] - 4);
                //Console.WriteLine("cetvrti {0} {1}", suma, suma2);
            }
            Console.WriteLine(suma);
        }
    }
}