n,m=list(map(int,input().split()))

d=[list(map(int,input().split())) for _ in range(m)]

c=[list(map(int,input().split())) for _ in range(n)]

ans0=[0]

ans1=[0]

ans2=[0]

for k in range(1,m+1):

    a0=0

    a1=0

    a2=0

    for i in range(n):

        for j in range(n):

            if (i+j+2)%3==0:

                a0+=d[c[i][j]-1][k-1]

            elif (i+j+2)%3==1:

                a1+=d[c[i][j]-1][k-1]

            else:

                a2+=d[c[i][j]-1][k-1]

    ans0.append(a0)

    ans1.append(a1)

    ans2.append(a2)

ans=float("inf")

import itertools

for x,y,z in itertools.permutations(list(range(1,m+1)),3):

    ans=min(ans,ans0[x]+ans1[y]+ans2[z])



print(ans)

N,C=list(map(int,input().split()))

d=[list(map(int,input().split())) for _ in range(C)]

c=[list(map(int,input().split())) for _ in range(N)]

x=[[0]*(C+1) for _ in range(3)]

for i in range(N):

    for j in range(N):

        x[(i+j+2)%3][c[i][j]]+=1



from itertools import permutations

ans=1<<32

for ch in permutations(list(range(1,C+1)),3):

    ans1=0

    for i in range(3):

        for j in range(1,C+1):

            ans1+=x[i][j]*d[j-1][ch[i]-1]

    ans=min(ans,ans1)

print(ans)

n, m = list(map(int, input().split()))
d = [list(map(int, input().split())) for _ in range(m)]
c = [list(map(int, input().split())) for _ in range(n)]
ans0 = [0]
ans1 = [0]
ans2 = [0]
for k in range(1, m + 1):
    a0 = 0
    a1 = 0
    a2 = 0
    for i in range(n):
        for j in range(n):
            if (i + j + 2) % 3 == 0:
                a0 += d[c[i][j] - 1][k - 1]
            elif (i + j + 2) % 3 == 1:
                a1 += d[c[i][j] - 1][k - 1]
            else:
                a2 += d[c[i][j] - 1][k - 1]
    ans0.append(a0)
    ans1.append(a1)
    ans2.append(a2)
ans = float("inf")
import itertools

for x, y, z in itertools.permutations(list(range(1, m + 1)), 3):
    ans = min(ans, ans0[x] + ans1[y] + ans2[z])
print(ans)

N, C = list(map(int, input().split()))
d = [list(map(int, input().split())) for _ in range(C)]
c = [list(map(int, input().split())) for _ in range(N)]
x = [[0] * (C + 1) for _ in range(3)]
for i in range(N):
    for j in range(N):
        x[(i + j + 2) % 3][c[i][j]] += 1
from itertools import permutations

ans = 1 << 32
for ch in permutations(list(range(1, C + 1)), 3):
    ans1 = 0
    for i in range(3):
        for j in range(1, C + 1):
            ans1 += x[i][j] * d[j - 1][ch[i] - 1]
    ans = min(ans, ans1)
print(ans)

-n, m = list(map(int, input().split())) -d = [list(map(int, input().split())) for _ in range(m)] -c = [list(map(int, input().split())) for _ in range(n)] -ans0 = [0] -ans1 = [0] -ans2 = [0] -for k in range(1, m + 1): -    a0 = 0 -    a1 = 0 -    a2 = 0 -    for i in range(n): -        for j in range(n): -            if (i + j + 2) % 3 == 0: -                a0 += d[c[i][j] - 1][k - 1] -            elif (i + j + 2) % 3 == 1: -                a1 += d[c[i][j] - 1][k - 1] -            else: -                a2 += d[c[i][j] - 1][k - 1] -    ans0.append(a0) -    ans1.append(a1) -    ans2.append(a2) -ans = float("inf") -import itertools +N, C = list(map(int, input().split())) +d = [list(map(int, input().split())) for _ in range(C)] +c = [list(map(int, input().split())) for _ in range(N)] +x = [[0] * (C + 1) for _ in range(3)] +for i in range(N): +    for j in range(N): +        x[(i + j + 2) % 3][c[i][j]] += 1 +from itertools import permutations -for x, y, z in itertools.permutations(list(range(1, m + 1)), 3): -    ans = min(ans, ans0[x] + ans1[y] + ans2[z]) +ans = 1 << 32 +for ch in permutations(list(range(1, C + 1)), 3): +    ans1 = 0 +    for i in range(3): +        for j in range(1, C + 1): +            ans1 += x[i][j] * d[j - 1][ch[i] - 1] +    ans = min(ans, ans1)