Buggy python submission (verdict: Time Limit Exceeded):
N,M = map(int,input().split())
D = [list(map(int,input().split())) for i in range(M)]
C = [list(map(int,input().split())) for i in range(N)]

ans = float("inf")

for i in range(M) :
    for j in range(M) :
        if i == j :
            continue
        
        for k in range(M) :
            if (i == k) or (j == k) :
                continue
            calc = 0
            for y in range(N) :
                for x in range(N) :
                    if (y+x)%3 == 0 :
                        calc += D[C[y][x]-1][i]
                    elif (y+x)%3 == 1 :
                        calc += D[C[y][x]-1][j]
                    else :
                        calc += D[C[y][x]-1][k]
            ans = min(ans,calc)
print(ans)


Fixed submission:
N,C = map(int,input().split())
D = [list(map(int,input().split())) for i in range(C)]
L = [list(map(int,input().split())) for i in range(N)]

d0 = {}
d1 = {}
d2 = {}

for i in range(N) :
    for j in range(N) :
        before = L[i][j] - 1
        
        if (i+j) % 3 == 0 :
            if before not in d0 :
                d0[before] = 1
            else :
                d0[before] += 1

        elif (i+j) % 3 == 1 :
            if before not in d1 :
                d1[before] = 1
            else :
                d1[before] += 1

        else :
            if before not in d2 :
                d2[before] = 1
            else :
                d2[before] += 1



A0 = []
A1 = []
A2 = []

for after in range(C) :
    calc = 0
    for before in d0 :
        calc += D[before][after] * d0[before]
    A0.append([calc,after])

for after in range(C) :
    calc = 0
    for before in d1 :
        calc += D[before][after] * d1[before]
    A1.append([calc,after])

for after in range(C) :
    calc = 0
    for before in d2 :
        calc += D[before][after] * d2[before]
    A2.append([calc,after])
    
A0 = sorted(A0,key=lambda x:x[0],reverse=False)[:3]
A1 = sorted(A1,key=lambda x:x[0],reverse=False)[:3]
A2 = sorted(A2,key=lambda x:x[0],reverse=False)[:3]
B = [A0,A1,A2]

ans = float("inf")

for i in range(3) :
    for j in range(3) :
        for k in range(3) :
            if (B[0][i][1] - B[1][j][1]) * (B[1][j][1] - B[2][k][1]) * (B[2][k][1] - B[0][i][1]) == 0 :
                continue
            
            ans = min(ans, B[0][i][0] + B[1][j][0] + B[2][k][0])

print(ans)

Buggy python submission (verdict: Runtime Error):
N,C = map(int,input().split())
D = [list(map(int,input().split())) for i in range(C)]
A = [list(map(int,input().split())) for i in range(N)]

ans = float("inf")

L = []
for i in range(1,C+1,3) :
    L.append([i,i+1,i+2])
    L.append([i,i+2,i+1])
    L.append([i+1,i,i+2])
    L.append([i+1,i+2,i])
    L.append([i+2,i,i+1])
    L.append([i+2,i+1,i])


for i in range(len(L)) :
    p,q,r = L[i]
    
    calc = 0
    
    for j in range(N) :
        for k in range(N) :
            if (j+k) % 3 == 0 :
                calc += D[A[j][k]-1][p-1]
            elif (j+k) % 3 == 1 :
                calc += D[A[j][k]-1][q-1]
            else :
                calc += D[A[j][k]-1][r-1]
    ans = min(ans,calc)
print(ans)



Fixed submission:
N,C = map(int,input().split())
D = [list(map(int,input().split())) for i in range(C)]
L = [list(map(int,input().split())) for i in range(N)]

d0 = {}
d1 = {}
d2 = {}

for i in range(N) :
    for j in range(N) :
        before = L[i][j] - 1
        
        if (i+j) % 3 == 0 :
            if before not in d0 :
                d0[before] = 1
            else :
                d0[before] += 1

        elif (i+j) % 3 == 1 :
            if before not in d1 :
                d1[before] = 1
            else :
                d1[before] += 1

        else :
            if before not in d2 :
                d2[before] = 1
            else :
                d2[before] += 1



A0 = []
A1 = []
A2 = []

for after in range(C) :
    calc = 0
    for before in d0 :
        calc += D[before][after] * d0[before]
    A0.append([calc,after])

for after in range(C) :
    calc = 0
    for before in d1 :
        calc += D[before][after] * d1[before]
    A1.append([calc,after])

for after in range(C) :
    calc = 0
    for before in d2 :
        calc += D[before][after] * d2[before]
    A2.append([calc,after])
    
A0 = sorted(A0,key=lambda x:x[0],reverse=False)[:3]
A1 = sorted(A1,key=lambda x:x[0],reverse=False)[:3]
A2 = sorted(A2,key=lambda x:x[0],reverse=False)[:3]
B = [A0,A1,A2]

ans = float("inf")

for i in range(3) :
    for j in range(3) :
        for k in range(3) :
            if (B[0][i][1] - B[1][j][1]) * (B[1][j][1] - B[2][k][1]) * (B[2][k][1] - B[0][i][1]) == 0 :
                continue
            
            ans = min(ans, B[0][i][0] + B[1][j][0] + B[2][k][0])

print(ans)