Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np
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)]

strange = np.array([[0]*C for _ in range(3)])
for i in range(N):
    for j in range(N):
        strange[(i+j)%3] += D[c[i][j]-1]

ans = 1000*N**2
for c1 in range(C):
    for c2 in range(C):
        for c3 in range(C):
            if c1 != c2 and c2 != c3 and c3 != c1:
                ans = min(ans,strange[0][c1]+strange[1][c2]+strange[2][c3])
print(ans)

Fixed submission:
import numpy as np
D = []
c = []
d0 = {}
d1 = {}
d2 = {}

N,C = map(int,input().split())
a = np.zeros((3,C))
for i in range(C):
    D.append(list(map(int,input().split())))
for i in range(N):
    c.append(list(map(int,input().split())))

D = np.array(D)
c = np.array(c)

for i in range(N):
    for j in range(N):
        a[(i+j)%3] += D[c[i][j]-1]

l = []
for C1 in range(C):
    for C2 in range(C):
        for C3 in range(C):
            if C1 != C2 and C2 != C3 and C3 != C1:
                l.append(a[0][C1]+a[1][C2]+a[2][C3])

print(int(min(l)))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
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)]

strange = [defaultdict(lambda:0) for _ in range(3)]
for i in range(N):
    for j in range(N):
        for color in range(C):
            strange[(i+j)%3][color] += D[c[i][j]-1][color]

ans = 1000*N**2
ref = 0
for c1 in range(C):
    for c2 in range(C):
        for c3 in range(C):
            if c1 != c2 and c2 != c3 and c3 != c1:
                ans = min(ans,strange[0][c1]+strange[1][c2]+strange[2][c3])
print(ans)

Fixed submission:
import numpy as np
D = []
c = []
d0 = {}
d1 = {}
d2 = {}

N,C = map(int,input().split())
a = np.zeros((3,C))
for i in range(C):
    D.append(list(map(int,input().split())))
for i in range(N):
    c.append(list(map(int,input().split())))

D = np.array(D)
c = np.array(c)

for i in range(N):
    for j in range(N):
        a[(i+j)%3] += D[c[i][j]-1]

l = []
for C1 in range(C):
    for C2 in range(C):
        for C3 in range(C):
            if C1 != C2 and C2 != C3 and C3 != C1:
                l.append(a[0][C1]+a[1][C2]+a[2][C3])

print(int(min(l)))

Buggy python submission (verdict: Time Limit Exceeded):
D = []
c = []
d0 = {}
d1 = {}
d2 = {}
N,C = map(int,input().split())
for i in range(C):
    D.append(list(map(int,input().split())))
for i in range(N):
    c.append(list(map(int,input().split())))

for i in range(C):
    d0[i+1]=0
    d1[i+1]=0
    d2[i+1]=0

for i in range(N):
    for j in range(N):
        for k in range(C):
            if (i+j)%3==0:
                d0[k+1]+=D[c[i][j]-1][k]
            elif (i+j)%3==1:
                d1[k+1]+=D[c[i][j]-1][k]
            else:
                d2[k+1]+=D[c[i][j]-1][k]
l = []
for C1 in range(C):
    for C2 in range(C):
        for C3 in range(C):
            if C1 != C2 and C2 != C3 and C3 != C1:
                l.append(d0[C1+1]+d1[C2+1]+d2[C3+1])

print(min(l))

Fixed submission:
import numpy as np
D = []
c = []
d0 = {}
d1 = {}
d2 = {}

N,C = map(int,input().split())
a = np.zeros((3,C))
for i in range(C):
    D.append(list(map(int,input().split())))
for i in range(N):
    c.append(list(map(int,input().split())))

D = np.array(D)
c = np.array(c)

for i in range(N):
    for j in range(N):
        a[(i+j)%3] += D[c[i][j]-1]

l = []
for C1 in range(C):
    for C2 in range(C):
        for C3 in range(C):
            if C1 != C2 and C2 != C3 and C3 != C1:
                l.append(a[0][C1]+a[1][C2]+a[2][C3])

print(int(min(l)))

Buggy python submission (verdict: Time Limit Exceeded):
inf = 1000*500*500
D = []
c = []
d = {}
N,C = map(int,input().split())
for i in range(C):
    D.append(list(map(int,input().split())))
for i in range(N):
    c.append(list(map(int,input().split())))

for C1 in range(C):
    for C2 in range(C):
        for C3 in range(C):
            if C1 != C2 and C2 != C3 and C3 != C1:
                d[(C1+1,C2+1,C3+1)]=0
            else:
                d[C1+1,C2+1,C3+1]=inf

for i in range(N):
    for j in range(N):
        for C1 in range(C):
            for C2 in range(C):
                for C3 in range(C):
                    if (i+j)%3==0:
                        d[(C1+1,C2+1,C3+1)]+=D[c[i][j]-1][C1]
                    elif (i+j)%3==1:
                        d[(C1+1,C2+1,C3+1)]+=D[c[i][j]-1][C2]
                    else:
                        d[(C1+1,C2+1,C3+1)]+=D[c[i][j]-1][C3]

l = []
for x in d:
    l.append(d[x])
print(min(l))


Fixed submission:
import numpy as np
D = []
c = []
d0 = {}
d1 = {}
d2 = {}

N,C = map(int,input().split())
a = np.zeros((3,C))
for i in range(C):
    D.append(list(map(int,input().split())))
for i in range(N):
    c.append(list(map(int,input().split())))

D = np.array(D)
c = np.array(c)

for i in range(N):
    for j in range(N):
        a[(i+j)%3] += D[c[i][j]-1]

l = []
for C1 in range(C):
    for C2 in range(C):
        for C3 in range(C):
            if C1 != C2 and C2 != C3 and C3 != C1:
                l.append(a[0][C1]+a[1][C2]+a[2][C3])

print(int(min(l)))

Buggy python submission (verdict: Runtime Error):
from math import inf
D = []
c = []
d = {}
N,C = map(int,input().split())
for i in range(C):
    D.append(list(map(int,input().split())))
for i in range(N):
    c.append(list(map(int,input().split())))

for C1 in range(C):
    for C2 in range(C):
        for C3 in range(C):
            if C1 != C2 and C2 != C3 and C3 != C1:
                d[(C1+1,C2+1,C3+1)]=0
            else:
                d[C1+1,C2+1,C3+1]=inf

for i in range(N):
    for j in range(N):
        for C1 in range(C):
            for C2 in range(C):
                for C3 in range(C):
                    if (i+j)%3==0:
                        d[(C1+1,C2+1,C3+1)]+=D[c[i][j]-1][C1]
                    elif (i+j)%3==1:
                        d[(C1+1,C2+1,C3+1)]+=D[c[i][j]-1][C2]
                    else:
                        d[(C1+1,C2+1,C3+1)]+=D[c[i][j]-1][C3]

l = []
for x in d:
    l.append(d[x])
print(min(l))

Fixed submission:
import numpy as np
D = []
c = []
d0 = {}
d1 = {}
d2 = {}

N,C = map(int,input().split())
a = np.zeros((3,C))
for i in range(C):
    D.append(list(map(int,input().split())))
for i in range(N):
    c.append(list(map(int,input().split())))

D = np.array(D)
c = np.array(c)

for i in range(N):
    for j in range(N):
        a[(i+j)%3] += D[c[i][j]-1]

l = []
for C1 in range(C):
    for C2 in range(C):
        for C3 in range(C):
            if C1 != C2 and C2 != C3 and C3 != C1:
                l.append(a[0][C1]+a[1][C2]+a[2][C3])

print(int(min(l)))

Buggy python submission (verdict: Runtime Error):
from math import inf
D = []
c = []
d = {}
N,C = map(int,input().split())
for i in range(C):
    D.append(list(map(int,input().split())))
for i in range(N):
    c.append(list(map(int,input().split())))

for C1 in range(C):
    for C2 in range(C):
        for C3 in range(C):
            if C1 != C2 and C2 != C3 and C3 != C1:
                d[(C1+1,C2+1,C3+1)]=0
            else:
                d[C1+1,C2+1,C3+1]=inf

for i in range(N):
    for j in range(N):
        for C1 in range(C):
            for C2 in range(C):
                for C3 in range(C):
                    if (i+j)%3==0:
                        d[(C1+1,C2+1,C3+1)]+=D[c[i][j]-1][C1]
                    elif (i+j)%3==1:
                        d[(C1+1,C2+1,C3+1)]+=D[c[i][j]-1][C2]
                    else:
                        d[(C1+1,C2+1,C3+1)]+=D[c[i][j]-1][C3]

l = []
for x in d:
    l.append(d[x])
print(min(l))

Fixed submission:
import numpy as np
D = []
c = []
d0 = {}
d1 = {}
d2 = {}

N,C = map(int,input().split())
a = np.zeros((3,C))
for i in range(C):
    D.append(list(map(int,input().split())))
for i in range(N):
    c.append(list(map(int,input().split())))

D = np.array(D)
c = np.array(c)

for i in range(N):
    for j in range(N):
        a[(i+j)%3] += D[c[i][j]-1]

l = []
for C1 in range(C):
    for C2 in range(C):
        for C3 in range(C):
            if C1 != C2 and C2 != C3 and C3 != C1:
                l.append(a[0][C1]+a[1][C2]+a[2][C3])

print(int(min(l)))