Buggy python submission (verdict: Runtime Error):
from itertools import*
n,c=map(int,input().split())
cost=[list(map(int,input().split()))for i in range(c)]
d=[[0]*c for i in range(3)]
for i in range(n):
    for j,v in enumerate(map(int,input().split())):
        d[(i+j)%3][v-1]+=1
print(min(sum(d[j][cmb[i]]*cost[i][j]for j in range(c)for i in range(3))for cmb in permutations(range(c),3)))

Fixed submission:
from itertools import*
n,c=map(int,input().split())
cost=[list(map(int,input().split()))for i in range(c)]
d=[[0]*c for i in range(3)]
for i in range(n):
    for j,v in enumerate(map(int,input().split())):
        d[(i+j)%3][v-1]+=1
print(min(sum(cost[j][cmb[i]]*d[i][j]for j in range(c)for i in range(3))for cmb in permutations(range(c),3)))

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import*
import sys
input=sys.stdin.readline
n,c=map(int,input().split())
cost=[list(map(int,input().split()))for i in range(c)]
d=[[],[],[]]
for i in range(n):
    for j,v in enumerate(map(int,input().split())):
        d[(i+j+2)%3].append(v-1)
ans=[]
for cmb in permutations(range(c),3):
    ans.append(sum(sum(cost[i][cmb[dindex]]for i in d[dindex])for dindex in range(3)))
print(min(ans))

Fixed submission:
from itertools import*
n,c=map(int,input().split())
cost=[list(map(int,input().split()))for i in range(c)]
d=[[0]*c for i in range(3)]
for i in range(n):
    for j,v in enumerate(map(int,input().split())):
        d[(i+j)%3][v-1]+=1
print(min(sum(cost[j][cmb[i]]*d[i][j]for j in range(c)for i in range(3))for cmb in permutations(range(c),3)))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import*
from itertools import*
import sys
input=sys.stdin.readline
n,c=map(int,input().split())
cost=[list(map(int,input().split()))for i in range(c)]
d=defaultdict(list)
for i in range(n):
    for j,v in enumerate(map(int,input().split())):
        d[(i+j+2)%3].append(v-1)
ans=[]
for cmb in permutations(range(c),3):
    ans.append(sum(sum(cost[i][cmb[dindex]]for i in d[dindex])for dindex in range(3)))
print(min(ans))

Fixed submission:
from itertools import*
n,c=map(int,input().split())
cost=[list(map(int,input().split()))for i in range(c)]
d=[[0]*c for i in range(3)]
for i in range(n):
    for j,v in enumerate(map(int,input().split())):
        d[(i+j)%3][v-1]+=1
print(min(sum(cost[j][cmb[i]]*d[i][j]for j in range(c)for i in range(3))for cmb in permutations(range(c),3)))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import*
from itertools import*
import sys
input=sys.stdin.readline
n,c=map(int,input().split())
cost=[list(map(int,input().split()))for i in range(c)]
d=defaultdict(list)
for i in range(n):
    for j,v in enumerate(map(int,input().split())):
        d[(i+j+2)%3].append(v-1)
ans=[]
for cmb in permutations(range(c),3):
    ans.append(sum(sum(cost[i][cmb[dindex]]for i in d[dindex])for dindex in range(3)))
print(min(ans))

Fixed submission:
from itertools import*
n,c=map(int,input().split())
cost=[list(map(int,input().split()))for i in range(c)]
d=[[0]*c for i in range(3)]
for i in range(n):
    for j,v in enumerate(map(int,input().split())):
        d[(i+j)%3][v-1]+=1
print(min(sum(cost[j][cmb[i]]*d[i][j]for j in range(c)for i in range(3))for cmb in permutations(range(c),3)))

Buggy python submission (verdict: Time Limit Exceeded):
from collections import*
from itertools import*
n,c=map(int,input().split())
cost=[list(map(int,input().split()))for i in range(c)]
grid=[list(map(int,input().split()))for i in range(n)]
d=defaultdict(list)
for i in range(1,n+1):
    for j in range(1,n+1):
        d[(i+j)%3].append(grid[i-1][j-1]-1)
ans=[]
for cmb in permutations(range(c),3):
    ans.append(sum(sum(cost[i][cmb[dindex]]for i in d[dindex])for dindex in range(3)))
print(min(ans))

Fixed submission:
from itertools import*
n,c=map(int,input().split())
cost=[list(map(int,input().split()))for i in range(c)]
d=[[0]*c for i in range(3)]
for i in range(n):
    for j,v in enumerate(map(int,input().split())):
        d[(i+j)%3][v-1]+=1
print(min(sum(cost[j][cmb[i]]*d[i][j]for j in range(c)for i in range(3))for cmb in permutations(range(c),3)))

Buggy python submission (verdict: Runtime Error):
from collections import*
from itertools import*
n,c=map(int,input().split())
cost=defaultdict(int)
for i in range(1,c+1):
    for k,v in enumerate(map(int,input().split())):
        cost[(k+1,i)]=v
#print(cost)

dwatta=defaultdict(list)
l=set()
for i in range(n):
    for j,v in enumerate(map(int,input().split())):
        mod=(i+j+2)%3
        dwatta[mod]+=[(v,(i,j))]
        l.add(v)
#print(l)
#print(dwatta) #割った値、座標、色
c=[]
for i in combinations(l,3):
    for s in permutations(i):
        ccc=0
        for k,v in dwatta.items():
            for f,t in enumerate([v]):
                for a,b in t:
                    if a!=s[f]:
                        ccc+=cost[(a,s[f])]
        c.append(ccc)
print(min(c))
        
            

Fixed submission:
from itertools import*
n,c=map(int,input().split())
cost=[list(map(int,input().split()))for i in range(c)]
d=[[0]*c for i in range(3)]
for i in range(n):
    for j,v in enumerate(map(int,input().split())):
        d[(i+j)%3][v-1]+=1
print(min(sum(cost[j][cmb[i]]*d[i][j]for j in range(c)for i in range(3))for cmb in permutations(range(c),3)))