n = int(eval(input()))

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

B = [e[:] for e in A]



for k in range(n):

    for i in range(n):

        for j in range(n):

            B[i][j] = min(B[i][k] + B[k][j], B[i][j])

ok = 1

for i in range(n):

    for j in range(n):

        if A[i][j] != B[i][j]:

            ok = 0

if not ok:

    print((-1))

    exit(0)



ans = 0

for i in range(n):

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

        need = 1

        for k in range(n):

            if i == k or j == k:

                continue

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

                need = 0

        if need:

            ans += A[i][j]

print(ans)

n = int(eval(input()))

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



ans = 0; ok = 1

for i in range(n):

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

        cost = A[i][j]

        for k in range(n):

            if i == k or j == k:

                continue

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

                ok = 0

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

                cost = 0

        ans += cost

print((ans if ok else -1))
n = int(eval(input()))
A = [list(map(int, input().split())) for i in range(n)]
B = [e[:] for e in A]
for k in range(n):
    for i in range(n):
        for j in range(n):
            B[i][j] = min(B[i][k] + B[k][j], B[i][j])
ok = 1
for i in range(n):
    for j in range(n):
        if A[i][j] != B[i][j]:
            ok = 0
if not ok:
    print((-1))
    exit(0)
ans = 0
for i in range(n):
    for j in range(i + 1, n):
        need = 1
        for k in range(n):
            if i == k or j == k:
                continue
            if A[i][k] + A[k][j] == A[i][j]:
                need = 0
        if need:
            ans += A[i][j]
print(ans)

n = int(eval(input()))
A = [list(map(int, input().split())) for i in range(n)]
ans = 0
ok = 1
for i in range(n):
    for j in range(i + 1, n):
        cost = A[i][j]
        for k in range(n):
            if i == k or j == k:
                continue
            if A[i][k] + A[k][j] < A[i][j]:
                ok = 0
            elif A[i][k] + A[k][j] == A[i][j]:
                cost = 0
        ans += cost
print((ans if ok else -1))

-B = [e[:] for e in A] -for k in range(n): -    for i in range(n): -        for j in range(n): -            B[i][j] = min(B[i][k] + B[k][j], B[i][j]) +ans = 0 -    for j in range(n): -        if A[i][j] != B[i][j]: -            ok = 0 -if not ok: -    print((-1)) -    exit(0) -ans = 0 -for i in range(n): -        need = 1 +        cost = A[i][j] -            if A[i][k] + A[k][j] == A[i][j]: -                need = 0 -        if need: -            ans += A[i][j] -print(ans) +            if A[i][k] + A[k][j] < A[i][j]: +                ok = 0 +            elif A[i][k] + A[k][j] == A[i][j]: +                cost = 0 +        ans += cost +print((ans if ok else -1))