using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using static System.Math;
using static System.Array;
using static AtCoder.Tool;
namespace AtCoder
{
    class AC
    {
        const int MOD = 1000000007;
        const int INF = int.MaxValue / 2;
        const long SINF = long.MaxValue / 2;
        const double EPS = 1e-8;
        static readonly int[] dx = { 0, 1, 0, -1 };
        static readonly int[] dy = { 1, 0, -1, 0 };

        
        static void Main(string[] args)
        {
            //var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            //Console.SetOut(sw);
            var cin = new Scanner();

            var p = cin.SplitRead();
            int N = p[0];
            int C = p[1];
            var D = new int[C,C];
            var c = new int[N, N];

            var grid = new List<List<int>>();
            for (var i = 0; i < 3; i++) grid.Add(new List<int>());

            for (var i = 0; i < C; i++)
            {
                var dd = cin.SplitRead();
                for(var j = 0; j < C; j++)
                {
                    D[i, j] = dd[j];
                }
            }
            for(var i = 0; i < N; i++)
            {
                var cc = cin.SplitRead();
                for(var j = 0; j < N; j++)
                {
                    c[i, j] = cc[j];
                    grid[(i + j + 2) % 3].Add(c[i, j]);
                }
            }

            var color = new int[3, C];
            for (var i = 0; i < 3; i++) for (var j = 0; j < C; j++) color[i, j] = 0;
            for(var i = 0; i < 3; i++)
            {
                for(var j = 0; j < grid[i].Count(); j++)
                {
                    color[i, grid[i][j] - 1]++;
                }
            }

            long ans = INF;
            for(var c1 = 0; c1 < C; c1++)
            {
                for(var c2 = 0; c2 < C; c2++)
                {
                    for(var c3 = 0; c3 < C; c3++)
                    {
                        if (c1 == c2 || c2 == c3 || c3 == c1) { continue; }
                        long dif = 0;
                        for(var j = 0; j < C; j++)
                        {
                            dif += D[j, c1] * color[0, j];
                            dif += D[j, c2] * color[1, j];
                            dif += D[j, c3] * color[2, j];
                        }
                        //Console.WriteLine(dif);
                        ans = Min(ans, dif);
                       
                    }
                }
            }

            Console.WriteLine(ans);


            
            //Console.Out.Flush();
        }

        
    }

    

    

    public class Scanner
    {
        public int[] SplitRead()
        {
            int[] array = Console.ReadLine().Split().Select(int.Parse).ToArray();
            return array;
        }
        public long[] SplitReadL()
        {
            long[] array = Console.ReadLine().Split().Select(long.Parse).ToArray();
            return array;
        }
    }

    public static class Tool
    {
        static public void Initialize<T>(ref T[] array, T initialvalue)
        {
            for (var i = 0; i < array.Length; i++)
            {
                array[i] = initialvalue;
            }
        }
    }

    public class PrimeList
    {
        private bool[] isprime;
        private List<int> primelist;

        public PrimeList(int n)
        {
            if (n < 2) { return; }
            primelist = new List<int>();
            isprime = new bool[n + 1];
            for (var i = 0; i <= n; i++)
            {
                isprime[i] = i != 0 && i != 1;
            }

            for (var i = 2; i <= n; i++)
            {
                if (!isprime[i]) { continue; }
                primelist.Add(i);

                int c = i;
                while (c + i <= n)
                {
                    c += i;
                    isprime[c] = false;
                }
            }
        }

        public bool IsPrime(int n)
        {
            return isprime[n];
        }

        public List<int> GetPrimeList()
        {
            return primelist;
        }

    }
}


Measured memory_bytes: 21332