Buggy python submission (verdict: Time Limit Exceeded):
import sys

def solve():
    input = sys.stdin.readline
    N, C = map(int, input().split())
    D = [[int(d) for d in input().split()] for _ in range(C)]
    col = [[int(i) - 1 for i in input().split()] for _ in range(N)]
    minD = 10 ** 20
    for i in range(C):
        for j in range(C):
            if i == j: continue
            for k in range(C):
                if j != k and k != i:
                    this_D = 0
                    changed = [i, j, k]
                    for ni in range(N):
                        for nj in range(N):
                            this_D += D[col[ni][nj]][changed[(ni + nj) % 3]]
                    minD = min(minD, this_D)
    print(minD)

    return 0

if __name__ == "__main__":
    solve()

Fixed submission:
from collections import defaultdict

N, C = map(int, input().split())
D = [[int(i) for i in input().split()] for c in range(C)]
Ini = [[int(i) for i in input().split()] for n in range(N)]
C0, C1, C2 = defaultdict(int), defaultdict(int), defaultdict(int)
D0, D1, D2 = [[0, d] for d in range(C)], [[0, d] for d in range(C)], [[0, d] for d in range(C)]
for i in range(N):
    for j in range(N):
        if (i+j+2)%3 == 0:
            C0[Ini[i][j] - 1] += 1
        elif (i+j+2)%3 == 1:
            C1[Ini[i][j] - 1] += 1
        else:
            C2[Ini[i][j] - 1] += 1

for i in range(C):
    for c in C0:
        D0[i][0] += D[c][i] * C0[c]
    for c in C1:
        D1[i][0] += D[c][i] * C1[c]
    for c in C2:
        D2[i][0] += D[c][i] * C2[c]

D0.sort()
D1.sort()
D2.sort()
Ans = float("inf")
for i in range(3):
    I = D0[i]
    for j in range(3):
        J = D1[j]
        for k in range(3):
            K = D2[k]
            if I[1] != J[1] and J[1] != K[1] and K[1] != I[1]:
                Ans = min(Ans, I[0] + J[0] + K[0])
print(Ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

def solve():
    input = sys.stdin.readline
    N, C = map(int, input().split())
    D = [[int(d) for d in input().split()] for _ in range(C)]
    col = [[int(i) - 1 for i in input().split()] for _ in range(N)]
    minD = 10 ** 20
    for i in range(C):
        for j in range(C):
            for k in range(C):
                if i != j and j != k and k != i:
                    this_D = 0
                    changed = [i, j, k]
                    for ni in range(N):
                        for nj in range(N):
                            if col[ni][nj] != changed[(ni + nj) % 3]:
                                this_D += D[col[ni][nj]][changed[(ni + nj) % 3]]
                    minD = min(minD, this_D)
    print(minD)

    return 0

if __name__ == "__main__":
    solve()

Fixed submission:
from collections import defaultdict

N, C = map(int, input().split())
D = [[int(i) for i in input().split()] for c in range(C)]
Ini = [[int(i) for i in input().split()] for n in range(N)]
C0, C1, C2 = defaultdict(int), defaultdict(int), defaultdict(int)
D0, D1, D2 = [[0, d] for d in range(C)], [[0, d] for d in range(C)], [[0, d] for d in range(C)]
for i in range(N):
    for j in range(N):
        if (i+j+2)%3 == 0:
            C0[Ini[i][j] - 1] += 1
        elif (i+j+2)%3 == 1:
            C1[Ini[i][j] - 1] += 1
        else:
            C2[Ini[i][j] - 1] += 1

for i in range(C):
    for c in C0:
        D0[i][0] += D[c][i] * C0[c]
    for c in C1:
        D1[i][0] += D[c][i] * C1[c]
    for c in C2:
        D2[i][0] += D[c][i] * C2[c]

D0.sort()
D1.sort()
D2.sort()
Ans = float("inf")
for i in range(3):
    I = D0[i]
    for j in range(3):
        J = D1[j]
        for k in range(3):
            K = D2[k]
            if I[1] != J[1] and J[1] != K[1] and K[1] != I[1]:
                Ans = min(Ans, I[0] + J[0] + K[0])
print(Ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys

def solve():
    input = sys.stdin.readline
    N, C = map(int, input().split())
    D = [[int(d) for d in input().split()] for _ in range(C)]
    col = [[int(i) - 1 for i in input().split()] for _ in range(N)]
    minD = 10 ** 20
    for i in range(C):
        for j in range(C):
            for k in range(C):
                if i != j and j != k and k != i:
                    this_D = 0
                    changed = [i, j, k]
                    for ni in range(N):
                        for nj in range(N):
                            if col[ni][nj] != changed[(ni + nj) % 3]:
                                this_D += D[col[ni][nj]][changed[(ni + nj) % 3]]
                    minD = min(minD, this_D)
    print(minD)

    return 0

if __name__ == "__main__":
    solve()

Fixed submission:
from collections import defaultdict

N, C = map(int, input().split())
D = [[int(i) for i in input().split()] for c in range(C)]
Ini = [[int(i) for i in input().split()] for n in range(N)]
C0, C1, C2 = defaultdict(int), defaultdict(int), defaultdict(int)
D0, D1, D2 = [[0, d] for d in range(C)], [[0, d] for d in range(C)], [[0, d] for d in range(C)]
for i in range(N):
    for j in range(N):
        if (i+j+2)%3 == 0:
            C0[Ini[i][j] - 1] += 1
        elif (i+j+2)%3 == 1:
            C1[Ini[i][j] - 1] += 1
        else:
            C2[Ini[i][j] - 1] += 1

for i in range(C):
    for c in C0:
        D0[i][0] += D[c][i] * C0[c]
    for c in C1:
        D1[i][0] += D[c][i] * C1[c]
    for c in C2:
        D2[i][0] += D[c][i] * C2[c]

D0.sort()
D1.sort()
D2.sort()
Ans = float("inf")
for i in range(3):
    I = D0[i]
    for j in range(3):
        J = D1[j]
        for k in range(3):
            K = D2[k]
            if I[1] != J[1] and J[1] != K[1] and K[1] != I[1]:
                Ans = min(Ans, I[0] + J[0] + K[0])
print(Ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, C = map(int, input().split())
D = [[int(i) for i in input().split()] for c in range(C)]
Ini = [[int(i) for i in input().split()] for n in range(N)]
Mod0, Mod1, Mod2 = [[0, c] for c in range(C)], [[0, c] for c in range(C)], [[0, c] for c in range(C)]

for i in range(N):
    for j in range(N):
        k = i+j+2
        if k%3==0:
            for c in range(C):
                Mod0[c][0] += D[Ini[i][j]-1][c]
        elif k%3==1:
            for c in range(C):
                Mod1[c][0] += D[Ini[i][j]-1][c]
        else:
            for c in range(C):
                Mod2[c][0] += D[Ini[i][j]-1][c]
Mod0.sort()
Mod1.sort()
Mod2.sort()
Ans = float("inf")
for a in range(3):
    A = Mod0[a]
    for b in range(3):
        B = Mod1[b]
        for c in range(3):
            C = Mod2[c]
            if A[1] != B[1] and B[1] != C[1] and C[1] != A[1]:
                Ans = min(Ans, A[0]+B[0]+C[0])
print(Ans)

Fixed submission:
from collections import defaultdict

N, C = map(int, input().split())
D = [[int(i) for i in input().split()] for c in range(C)]
Ini = [[int(i) for i in input().split()] for n in range(N)]
C0, C1, C2 = defaultdict(int), defaultdict(int), defaultdict(int)
D0, D1, D2 = [[0, d] for d in range(C)], [[0, d] for d in range(C)], [[0, d] for d in range(C)]
for i in range(N):
    for j in range(N):
        if (i+j+2)%3 == 0:
            C0[Ini[i][j] - 1] += 1
        elif (i+j+2)%3 == 1:
            C1[Ini[i][j] - 1] += 1
        else:
            C2[Ini[i][j] - 1] += 1

for i in range(C):
    for c in C0:
        D0[i][0] += D[c][i] * C0[c]
    for c in C1:
        D1[i][0] += D[c][i] * C1[c]
    for c in C2:
        D2[i][0] += D[c][i] * C2[c]

D0.sort()
D1.sort()
D2.sort()
Ans = float("inf")
for i in range(3):
    I = D0[i]
    for j in range(3):
        J = D1[j]
        for k in range(3):
            K = D2[k]
            if I[1] != J[1] and J[1] != K[1] and K[1] != I[1]:
                Ans = min(Ans, I[0] + J[0] + K[0])
print(Ans)