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

N, C = map(int, input().split())
D = [ [ int(x) for x in input().split() ] for _ in range(C) ]
CC = [ [ int(x)-1 for x in input().split() ] for _ in range(N) ]
cost = [ [ 0 for _ in range(C) ] for _ in range(3) ]

for c in range(C):
    for i in range(N):
        for j in range(N):
            cost[(i+j) % 3][c] += D[CC[i][j]][c]

ans = float("inf")
for c in itertools.permutations(range(C), 3):
    tmp = 0
    for j in range(3):
        tmp += cost[j][c[j]]

    ans = min(ans, tmp)

print(ans)

Fixed submission:
import itertools
 
 
def main():
    N, C = map(int, input().split())
    D = [list(map(int, input().split())) for _ in range(C)]
    CC = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(N)]
 
    cost = [[0 for _ in range(C)] for _ in range(3)]
    for c in range(C):
        for i in range(N):
            for j in range(N):
                cost[(i + j) % 3][c] += D[CC[i][j]][c]
 
    res = float("inf")
    color = [i for i in range(C)]
    for i, c in enumerate(list(itertools.permutations(color, 3))):
        tmp = 0
        for it in range(3):
            tmp += cost[it][c[it]]
        res = min(res, tmp)
 
    print(res)
 
main()

Buggy python submission (verdict: Time Limit Exceeded):
N, C = map(int, raw_input().split())
 
D = [ [int(i) for i in raw_input().split()] for _ in xrange(C) ]
c = [ [int(i) for i in raw_input().split()] for _ in xrange(N) ]
 
costD = [ [0, 0, 0] for _ in xrange(C)]
 
for c0 in xrange(C):
    for i in xrange(N):
        for j in xrange(N):
            costD[c0][(i+j)%3] += D[c[i][j]-1][c0]
 
kohoD = []
 
for c1 in xrange(C):
    for c2 in xrange(C):
        if c1 == c2:
            continue
        for c3 in xrange(C):
            if c1 == c3 or c2 == c3:
                continue
            kohoD += [(costD[c1][0] + costD[c2][1] + costD[c3][2])]
 
print(min(kohoD))

Fixed submission:
import itertools
 
 
def main():
    N, C = map(int, input().split())
    D = [list(map(int, input().split())) for _ in range(C)]
    CC = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(N)]
 
    cost = [[0 for _ in range(C)] for _ in range(3)]
    for c in range(C):
        for i in range(N):
            for j in range(N):
                cost[(i + j) % 3][c] += D[CC[i][j]][c]
 
    res = float("inf")
    color = [i for i in range(C)]
    for i, c in enumerate(list(itertools.permutations(color, 3))):
        tmp = 0
        for it in range(3):
            tmp += cost[it][c[it]]
        res = min(res, tmp)
 
    print(res)
 
main()

Buggy python submission (verdict: Runtime Error):
N, C = map(int, input().split())
 
D = [ [int(i) for i in input().split()] for _ in xrange(C) ]
c = [ [int(i) for i in input().split()] for _ in xrange(N) ]
 
costD = [ [0, 0, 0] for _ in xrange(C)]
 
for c0 in xrange(C):
    for i in xrange(N):
        for j in xrange(N):
            costD[c0][(i+j)%3] += D[c[i][j]-1][c0]
 
kohoD = []
 
for c1 in xrange(C):
    for c2 in xrange(C):
        if c1 == c2:
            continue
        for c3 in xrange(C):
            if c1 == c3 or c2 == c3:
                continue
            kohoD += [(costD[c1][0] + costD[c2][1] + costD[c3][2])]
 
print(min(kohoD))

Fixed submission:
import itertools
 
 
def main():
    N, C = map(int, input().split())
    D = [list(map(int, input().split())) for _ in range(C)]
    CC = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(N)]
 
    cost = [[0 for _ in range(C)] for _ in range(3)]
    for c in range(C):
        for i in range(N):
            for j in range(N):
                cost[(i + j) % 3][c] += D[CC[i][j]][c]
 
    res = float("inf")
    color = [i for i in range(C)]
    for i, c in enumerate(list(itertools.permutations(color, 3))):
        tmp = 0
        for it in range(3):
            tmp += cost[it][c[it]]
        res = min(res, tmp)
 
    print(res)
 
main()

Buggy python submission (verdict: Time Limit Exceeded):
N, C = map(int, input().split())

D = [ [int(i) for i in input().split()] for _ in range(C) ]
c = [ [int(i) for i in input().split()] for _ in range(N) ]

costD = [ [0, 0, 0] for _ in range(C)]

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

kohoD = []

for c1 in range(C):
    for c2 in range(C):
        if c1 == c2:
            continue
        for c3 in range(C):
            if c1 == c3 or c2 == c3:
                continue
            kohoD += [(costD[c1][0] + costD[c2][1] + costD[c3][2])]

print(min(kohoD))


Fixed submission:
import itertools
 
 
def main():
    N, C = map(int, input().split())
    D = [list(map(int, input().split())) for _ in range(C)]
    CC = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(N)]
 
    cost = [[0 for _ in range(C)] for _ in range(3)]
    for c in range(C):
        for i in range(N):
            for j in range(N):
                cost[(i + j) % 3][c] += D[CC[i][j]][c]
 
    res = float("inf")
    color = [i for i in range(C)]
    for i, c in enumerate(list(itertools.permutations(color, 3))):
        tmp = 0
        for it in range(3):
            tmp += cost[it][c[it]]
        res = min(res, tmp)
 
    print(res)
 
main()

Buggy python submission (verdict: Time Limit Exceeded):
N, C = map(int, input().split())

D = [ [int(i) for i in input().split()] for _ in range(C) ] 
c = [ [int(i) for i in input().split()] for _ in range(N) ] 

costD = [0 for _ in range(C)]

for c0 in range(C):
    tmpD = [0] * 3
    for i in range(N):
        for j in range(N):
            tmpD[(i+j)%3] += D[c[i][j]-1][c0]
    costD[c0] = tmpD

kohoD = []

for c1 in range(C):
    for c2 in range(C):
        if c1 == c2:
            continue
        for c3 in range(C):
            if c1 == c3 or c2 == c3:
                continue
            kohoD += [(costD[c1][0] + costD[c2][1] + costD[c3][2])]

print(min(kohoD))


Fixed submission:
import itertools
 
 
def main():
    N, C = map(int, input().split())
    D = [list(map(int, input().split())) for _ in range(C)]
    CC = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(N)]
 
    cost = [[0 for _ in range(C)] for _ in range(3)]
    for c in range(C):
        for i in range(N):
            for j in range(N):
                cost[(i + j) % 3][c] += D[CC[i][j]][c]
 
    res = float("inf")
    color = [i for i in range(C)]
    for i, c in enumerate(list(itertools.permutations(color, 3))):
        tmp = 0
        for it in range(3):
            tmp += cost[it][c[it]]
        res = min(res, tmp)
 
    print(res)
 
main()

Buggy python submission (verdict: Time Limit Exceeded):
N, C = map(int, input().split())

D = [ [int(i) for i in input().split()] for _ in range(C) ] 
c = [ [int(i) for i in input().split()] for _ in range(N) ] 

costD = [0 for _ in range(C)]

for c0 in range(C):
    tmpD = [0] * 3
    for i in range(N):
        for j in range(N):
            tmpD[(i+j)%3] += D[c[i][j]-1][c0]
    costD[c0] = tmpD

kohoD = []

for c1 in range(C):
    for c2 in range(C):
        if c1 == c2:
            continue
        for c3 in range(C):
            if c1 == c3 or c2 == c3:
                continue
            kohoD += [(costD[c1][0] + costD[c2][1] + costD[c3][2])]

print(sorted(kohoD)[0])


Fixed submission:
import itertools
 
 
def main():
    N, C = map(int, input().split())
    D = [list(map(int, input().split())) for _ in range(C)]
    CC = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(N)]
 
    cost = [[0 for _ in range(C)] for _ in range(3)]
    for c in range(C):
        for i in range(N):
            for j in range(N):
                cost[(i + j) % 3][c] += D[CC[i][j]][c]
 
    res = float("inf")
    color = [i for i in range(C)]
    for i, c in enumerate(list(itertools.permutations(color, 3))):
        tmp = 0
        for it in range(3):
            tmp += cost[it][c[it]]
        res = min(res, tmp)
 
    print(res)
 
main()

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

for _ in range(N):
    c.append([int(i) for i in input().split()])

costD = []
for c0 in range(C):
    tmpD = [0] * 3
    for i in range(N):
        for j in range(N):
            tmpD[(i+j)%3] += D[c[i][j]-1][c0]
    costD.append(tmpD)

kohoD = []
for c1 in range(C):
    for c2 in range(C):
        if c1 == c2:
            continue
        for c3 in range(C):
            if c1 == c3 or c2 == c3:
                continue
            kohoD.append(costD[c1][0] + costD[c2][1] + costD[c3][2])

kohoD.sort()
print(kohoD[0])

Fixed submission:
import itertools
 
 
def main():
    N, C = map(int, input().split())
    D = [list(map(int, input().split())) for _ in range(C)]
    CC = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(N)]
 
    cost = [[0 for _ in range(C)] for _ in range(3)]
    for c in range(C):
        for i in range(N):
            for j in range(N):
                cost[(i + j) % 3][c] += D[CC[i][j]][c]
 
    res = float("inf")
    color = [i for i in range(C)]
    for i, c in enumerate(list(itertools.permutations(color, 3))):
        tmp = 0
        for it in range(3):
            tmp += cost[it][c[it]]
        res = min(res, tmp)
 
    print(res)
 
main()

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

for _ in range(N):
    c.append([int(i) for i in input().split()])


ansD = []
for c1 in range(C):
    for c2 in range(C):
        if c1 == c2:
            continue
        for c3 in range(C):
            if c1 == c3 or c2 == c3:
                continue
            sumD = 0
            for i in range(N):
                for j in range(N):
                    if (i+j) % 3 == 0:
                        goodc = c1
                    if (i+j) % 3 == 1:
                        goodc = c2
                    if (i+j) % 3 == 2:
                        goodc = c3
                    sumD += D[c[i][j]-1][goodc]
            ansD.append(sumD)
ansD.sort()
print(ansD[0])

Fixed submission:
import itertools
 
 
def main():
    N, C = map(int, input().split())
    D = [list(map(int, input().split())) for _ in range(C)]
    CC = [list(map(lambda x: int(x) - 1, input().split())) for _ in range(N)]
 
    cost = [[0 for _ in range(C)] for _ in range(3)]
    for c in range(C):
        for i in range(N):
            for j in range(N):
                cost[(i + j) % 3][c] += D[CC[i][j]][c]
 
    res = float("inf")
    color = [i for i in range(C)]
    for i, c in enumerate(list(itertools.permutations(color, 3))):
        tmp = 0
        for it in range(3):
            tmp += cost[it][c[it]]
        res = min(res, tmp)
 
    print(res)
 
main()