using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;

namespace Competitive
{
    internal class Solution
    {
        public int N, C;
        public int[,] D;
        public int[,] color;

        public void Run()
        {
            // input
            {
                var line = Input.ReadIntArray();
                N = line[0];
                C = line[1];
                D = new int[C, C];
                color = new int[N, N];
            }

            for (int i = 0; i < C; i++)
            {
                var line = Input.ReadIntArray();
                for (int j = 0; j < C; j++)
                    D[i, j] = line[j]; // i => j
            }

            for (int y = 0; y < N; y++)
            {
                var line = Input.ReadIntArray();
                for (int x = 0; x < N; x++)
                    color[y, x] = line[x] - 1;
            }

            // Run
            var L = new List<List<int>>();
            for (int i = 0; i < 3; i++)
            {
                var lst = new List<int>();
                for (int y = 0; y < N; y++)
                for (int x = 0; x < N; x++)
                    if((y + x) % 3 == i) lst.Add(color[y, x]);

                L.Add(lst);
            }

            var S = new long[C, 3];
            for (int c = 0; c < C; c++)
            for (int m = 0; m < 3; m++)
            {
                long s = 0;
                for (int i = 0; i < L[m].Count; i++)
                {
                    s += D[L[m][i], c];
                }

                S[c, m] = s;
            }

            long ret = long.MaxValue;
            for (int c1 = 0; c1 < C; c1++)
            for (int c2 = 0; c2 < C; c2++)
            for (int c3 = 0; c3 < C; c3++)
            {
                if (c1 == c2 || c1 == c3 || c2 == c3) continue;
                long r = S[c1, 0] + S[c2, 1] + S[c3, 2];
                ret = Math.Min(ret, r);
            }

            /*
            long ret = long.MaxValue;
            for (int c1 = 0; c1 < C; c1++)
            for (int c2 = 0; c2 < C; c2++)
            for (int c3 = 0; c3 < C; c3++)
            {
                if(c1 == c2 || c1 == c3|| c2 == c3) continue;

                var cc = new int[]{c1, c2, c3};
                long r = 0;
                for (int y = 0; y < N; y++)
                for (int x = 0; x < N; x++)
                {
                    int c = cc[(y + x) % 3];
                    r += D[color[y, x], c];
                }

                ret = Math.Min(ret, r);
            }
            */

            Console.WriteLine(ret);

        }
    }

    // libs ----------
    

    // common ----------

    internal static class Input
    {
        public static string ReadString()
        {
            string line = Console.ReadLine();
            return line;
        }

        public static int ReadInt()
        {
            string line = Console.ReadLine();
            return int.Parse(line);
        }

        public static int[] ReadIntArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(int.Parse).ToArray();            
        }

        public static long ReadLong()
        {
            string line = Console.ReadLine();
            return long.Parse(line);
        }

        public static long[] ReadLongArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(long.Parse).ToArray();
        }

        public static double[] ReadDoubleArray()
        {
            string line = Console.ReadLine();
            return line.Split(' ').Select(double.Parse).ToArray();
        }
    }
    
    internal class Program
    {
        public static void Main(string[] args)
        {
            var s = new Solution();
            s.Run();
        }
    }
}

Measured memory_bytes: 21456