N = int(eval(input()))

A = [list(map(int,input().split())) for _ in range(N)]



flag = [[0] * N for _ in range(N)]

dist = [line[:] for line in A]



for k in range(N):

    for i in range(N):

        for j in range(N):

            if i == j or j == k or k == i:

                continue

            if dist[i][j] > dist[i][k] + dist[k][j]:

                dist[i][j] = dist[i][k] + dist[k][j]

            elif dist[i][j] < dist[i][k] + dist[k][j]:

                flag[i][j] += 1



if A != dist:

    print((-1))

    exit()



ans = 0

for i in range(N):

    for j in range(N):

        if flag[i][j] == N-2:

            ans += A[i][j]



ans //= 2

print(ans)
N = int(eval(input()))

A = [list(map(int,input().split())) for _ in range(N)]



flag = [[True] * N for _ in range(N)]



for i in range(N):

    for j in range(i+1, N):

        for k in range(N):

            if k == i or k == j:

                continue

            if A[i][j] > A[i][k] + A[k][j]:

                print((-1))

                exit()

            elif A[i][j] == A[i][k] + A[k][j]:

                flag[i][j] = False



ans = 0



for i in range(N):

    for j in range(i+1, N):

        if flag[i][j]:

            ans += A[i][j]



print(ans)
N = int(eval(input()))
A = [list(map(int, input().split())) for _ in range(N)]
flag = [[0] * N for _ in range(N)]
dist = [line[:] for line in A]
for k in range(N):
    for i in range(N):
        for j in range(N):
            if i == j or j == k or k == i:
                continue
            if dist[i][j] > dist[i][k] + dist[k][j]:
                dist[i][j] = dist[i][k] + dist[k][j]
            elif dist[i][j] < dist[i][k] + dist[k][j]:
                flag[i][j] += 1
if A != dist:
    print((-1))
    exit()
ans = 0
for i in range(N):
    for j in range(N):
        if flag[i][j] == N - 2:
            ans += A[i][j]
ans //= 2
print(ans)

N = int(eval(input()))
A = [list(map(int, input().split())) for _ in range(N)]
flag = [[True] * N for _ in range(N)]
for i in range(N):
    for j in range(i + 1, N):
        for k in range(N):
            if k == i or k == j:
                continue
            if A[i][j] > A[i][k] + A[k][j]:
                print((-1))
                exit()
            elif A[i][j] == A[i][k] + A[k][j]:
                flag[i][j] = False
ans = 0
for i in range(N):
    for j in range(i + 1, N):
        if flag[i][j]:
            ans += A[i][j]
print(ans)

-flag = [[0] * N for _ in range(N)] -dist = [line[:] for line in A] -for k in range(N): -    for i in range(N): -        for j in range(N): -            if i == j or j == k or k == i: +flag = [[True] * N for _ in range(N)] +for i in range(N): +    for j in range(i + 1, N): +        for k in range(N): +            if k == i or k == j: -            if dist[i][j] > dist[i][k] + dist[k][j]: -                dist[i][j] = dist[i][k] + dist[k][j] -            elif dist[i][j] < dist[i][k] + dist[k][j]: -                flag[i][j] += 1 -if A != dist: -    print((-1)) -    exit() +            if A[i][j] > A[i][k] + A[k][j]: +                print((-1)) +                exit() +            elif A[i][j] == A[i][k] + A[k][j]: +                flag[i][j] = False -    for j in range(N): -        if flag[i][j] == N - 2: +    for j in range(i + 1, N): +        if flag[i][j]: -ans //= 2