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

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

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


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


ans = 99999999999999999999
for p1 in range(C):
    for p2 in range(C):
        if p1 == p2:
            continue
        a = I[p1][0] + I[p2][1]
        for p3 in range(C):
            if p1 == p3 or p2 == p3:
                continue
            ans = min(ans, a + I[p3][2])

print(ans)


Fixed submission:
N, C = [int(x) for x in input().split()]
D = [[0] * C for _ in range(C)]
c = [[0] * N for _ in range(N)]

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

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

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

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

for p in range(3):
    for toC in range(C):
        for fromC in range(C):
            I[toC][p] += D[fromC][toC] * COL[fromC][p]

# I[toC][pattern] += D[c[i][j] - 1][toC]

ans = 99999999999999999999
for p1 in range(C):
    for p2 in range(C):
        if p1 == p2:
            continue
        a = I[p1][0] + I[p2][1]
        for p3 in range(C):
            if p1 == p3 or p2 == p3:
                continue
            ans = min(ans, a + I[p3][2])

print(ans)

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

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

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

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

ans = 99999999999999999999
for p1 in range(C):
    for p2 in range(C):
        if p1 == p2:
            continue
        a = I[p1][0] + I[p2][1]
        for p3 in range(C):
            if p1 == p3 or p2 == p3:
                continue
            ans = min(ans, a + I[p3][2])

print(ans)


Fixed submission:
N, C = [int(x) for x in input().split()]
D = [[0] * C for _ in range(C)]
c = [[0] * N for _ in range(N)]

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

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

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

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

for p in range(3):
    for toC in range(C):
        for fromC in range(C):
            I[toC][p] += D[fromC][toC] * COL[fromC][p]

# I[toC][pattern] += D[c[i][j] - 1][toC]

ans = 99999999999999999999
for p1 in range(C):
    for p2 in range(C):
        if p1 == p2:
            continue
        a = I[p1][0] + I[p2][1]
        for p3 in range(C):
            if p1 == p3 or p2 == p3:
                continue
            ans = min(ans, a + I[p3][2])

print(ans)

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

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

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

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

ans = 99999999999999999999
for p1 in range(C):
    for p2 in range(C):
        for p3 in range(C):
            if p1 == p3 or p2 == p3 or p1 == p2:
                continue
            ans = min(ans, I[p1][0] + I[p2][1] + I[p3][2])

print(ans)


Fixed submission:
N, C = [int(x) for x in input().split()]
D = [[0] * C for _ in range(C)]
c = [[0] * N for _ in range(N)]

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

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

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

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

for p in range(3):
    for toC in range(C):
        for fromC in range(C):
            I[toC][p] += D[fromC][toC] * COL[fromC][p]

# I[toC][pattern] += D[c[i][j] - 1][toC]

ans = 99999999999999999999
for p1 in range(C):
    for p2 in range(C):
        if p1 == p2:
            continue
        a = I[p1][0] + I[p2][1]
        for p3 in range(C):
            if p1 == p3 or p2 == p3:
                continue
            ans = min(ans, a + I[p3][2])

print(ans)