Buggy python submission (verdict: Runtime Error):
from itertools import permutations
from collections import deque
from collections import Counter
N, C = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(C)]
c = [list(map(int, input().split())) for _ in range(N)]

r0 = deque()
r1 = deque()
r2 = deque()
for i in range(N):
    for j in range(N):
        rest = (i+1+j+1) % 3
        if rest == 0:
            r0.append(c[i][j]-1)
        elif rest == 1:
            r1.append(c[i][j]-1)
        else:
            r2.append(c[i][j]-1)
c0 = Counter(r0)
c1 = Counter(r1)
c2 = Counter(r2)
ans = 10**10
for p in permutations([i for i in range(C)], 3):
    point = 0
    point += sum([d[k][p[0] * v] for k, v in c0.items()])
    point += sum([d[k][p[1] * v] for k, v in c1.items()])
    point += sum([d[k][p[2] * v] for k, v in c2.items()])
    ans = min(ans, point)
print(ans)


Fixed submission:
from itertools import permutations
from collections import deque
from collections import Counter
N, C = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(C)]
c = [list(map(int, input().split())) for _ in range(N)]

r0 = deque()
r1 = deque()
r2 = deque()
for i in range(N):
    for j in range(N):
        rest = (i+1+j+1) % 3
        if rest == 0:
            r0.append(c[i][j]-1)
        elif rest == 1:
            r1.append(c[i][j]-1)
        else:
            r2.append(c[i][j]-1)
c0 = Counter(r0)
c1 = Counter(r1)
c2 = Counter(r2)
ans = 10**10
for p in permutations([i for i in range(C)], 3):
    point = 0
    point += sum([d[k][p[0]]* v for k, v in c0.items()])
    point += sum([d[k][p[1]]* v for k, v in c1.items()])
    point += sum([d[k][p[2]]* v for k, v in c2.items()])
    ans = min(ans, point)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import permutations
N, C = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(C)]
c = [list(map(int, input().split())) for _ in range(N)]

r0 = []
r1 = []
r2 = []
for i in range(N):
    for j in range(N):
        rest = (i+1+j+1) % 3
        if rest == 0:
            r0.append(c[i][j]-1)
        elif rest == 1:
            r1.append(c[i][j]-1)
        else:
            r2.append(c[i][j]-1)
ans = 10**10
for p in permutations([i for i in range(C)], 3):
    point = 0
    point += sum([d[rest][p[0]] for rest in r0])
    point += sum([d[rest][p[1]] for rest in r1])
    point += sum([d[rest][p[2]] for rest in r2])
    ans = min(ans, point)
print(ans)


Fixed submission:
from itertools import permutations
from collections import deque
from collections import Counter
N, C = map(int, input().split())
d = [list(map(int, input().split())) for _ in range(C)]
c = [list(map(int, input().split())) for _ in range(N)]

r0 = deque()
r1 = deque()
r2 = deque()
for i in range(N):
    for j in range(N):
        rest = (i+1+j+1) % 3
        if rest == 0:
            r0.append(c[i][j]-1)
        elif rest == 1:
            r1.append(c[i][j]-1)
        else:
            r2.append(c[i][j]-1)
c0 = Counter(r0)
c1 = Counter(r1)
c2 = Counter(r2)
ans = 10**10
for p in permutations([i for i in range(C)], 3):
    point = 0
    point += sum([d[k][p[0]]* v for k, v in c0.items()])
    point += sum([d[k][p[1]]* v for k, v in c1.items()])
    point += sum([d[k][p[2]]* v for k, v in c2.items()])
    ans = min(ans, point)
print(ans)