Buggy python submission (verdict: Time Limit Exceeded):
import itertools
N = int(input())
A = [[int(_) for _ in input().split()] for _ in range(N)]
ans = sum(sum(a) for a in A) // 2
seen = set()
for i in range(N):
    for j in range(i + 1, N):
        for k in range(N):
            if A[i][j] > A[i][k] + A[j][k]:
                print(-1)
                exit()
            elif k == i or i == j or j == k:
                continue
            elif A[i][j] == A[i][k] + A[j][k]:
                ans -= A[i][j]
                break
print(ans)


Fixed submission:
import itertools
N = int(input())
A = [[int(_) for _ in input().split()] for _ in range(N)]
ans = sum(sum(a) for a in A) // 2
seen = set()
for i in range(N):
    for j in range(i + 1, N):
        for k in range(N):
            if A[i][j] > A[i][k] + A[j][k]:
                print(-1)
                exit()
            elif k == i or i == j or j == k:
                continue
            elif A[i][j] == A[i][k] + A[j][k]:
                ans -= A[i][j]
                break
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
N = int(input())
A = [[int(_) for _ in input().split()] for _ in range(N)]
ans = sum(sum(a) for a in A) // 2
seen = set()
for i in range(N):
    for j in range(i + 1, N):
        for k in range(N):
            if A[i][j] > A[i][k] + A[j][k]:
                print(-1)
                exit()
            elif k == i or i == j or j == k:
                continue
            elif A[i][j] == A[i][k] + A[j][k]:
                ans -= A[i][j]
                break
print(ans)


Fixed submission:
import itertools
N = int(input())
A = [[int(_) for _ in input().split()] for _ in range(N)]
ans = sum(sum(a) for a in A) // 2
seen = set()
for i in range(N):
    for j in range(i + 1, N):
        for k in range(N):
            if A[i][j] > A[i][k] + A[j][k]:
                print(-1)
                exit()
            elif k == i or i == j or j == k:
                continue
            elif A[i][j] == A[i][k] + A[j][k]:
                ans -= A[i][j]
                break
print(ans)

Buggy python submission (verdict: Runtime Error):
import itertools
N = int(input())
A = [[int(_) for _ in input().split()] for _ in range(N)]
ans = sum(sum(a) for a in A)
seen = set()
for k, i, j in itertools.product(range(N), repeat=3):
    if (i, j) in seen or k == i or i == j or j == k:
        continue
    elif A[i][j] > A[i][k] + A[j][k]:
        print(-1)
        exit()
    elif A[i][j] == A[i][k] + A[j][k]:
        ans -= A[i][j]
        seen.add((i, k))
        seen.add((k, i))
        seen.add((j, k))
        seen.add((k, j))
    seen.add(i, j)
    seen.add(j, i)
print(ans // 2)


Fixed submission:
import itertools
N = int(input())
A = [[int(_) for _ in input().split()] for _ in range(N)]
ans = sum(sum(a) for a in A) // 2
seen = set()
for i in range(N):
    for j in range(i + 1, N):
        for k in range(N):
            if A[i][j] > A[i][k] + A[j][k]:
                print(-1)
                exit()
            elif k == i or i == j or j == k:
                continue
            elif A[i][j] == A[i][k] + A[j][k]:
                ans -= A[i][j]
                break
print(ans)

Buggy python submission (verdict: Runtime Error):
import collections
N = int(input())
A = [[int(_) for _ in input().split()] for _ in range(N)]
B = [_.copy() for _ in A]
d_ij = collections.defaultdict(set)
for i in range(N - 1):
    for j in range(i + 1, N):
        d_ij[A[i][j]].add((i, j))
connected = collections.defaultdict(set)
ans = 0
for k in sorted(d_ij.keys()):
    for i, j in d_ij[k]:
        if i not in connected[j]:
            for p in connected[j]:
                if A[p][i] > A[p][j] + A[j][i]:
                    print(-1)
                    exit()
                elif A[p][i] == A[p][j] + A[j][i]:
                    B[p][i] = B[i][p] = 0
                    connected[p].add(i)
                    connected[i].add(p)
            for q in connected[i]:
                if A[q][j] > A[q][i] + A[i][j]:
                    print(-1)
                    exit()
                elif A[q][j] == A[q][i] + A[i][j]:
                    B[q][j] = B[j][q] = 0
                    connected[q].add(j)
                    connected[j].add(q)
            connected[i].add(j)
            connected[j].add(i)
print(sum(sum(_) for _ in B) // 2)


Fixed submission:
import itertools
N = int(input())
A = [[int(_) for _ in input().split()] for _ in range(N)]
ans = sum(sum(a) for a in A) // 2
seen = set()
for i in range(N):
    for j in range(i + 1, N):
        for k in range(N):
            if A[i][j] > A[i][k] + A[j][k]:
                print(-1)
                exit()
            elif k == i or i == j or j == k:
                continue
            elif A[i][j] == A[i][k] + A[j][k]:
                ans -= A[i][j]
                break
print(ans)