N = int(eval(input()))

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



ans = 0

for k in range(N):

    for i in range(N):

        for j in range(N):

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

                print((-1))

                exit()

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



for i in range(N-1):

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

        use = True

        for k in range(N):

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

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

                use = False

                break

        if use:

            ans += src[i][j]

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

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



ans = 0

for k in range(N):

    for i in range(N):

        for j in range(N):

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

                print((-1))

                exit()



for i in range(N-1):

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

        use = True

        for k in range(N):

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

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

                use = False

                break

        if use:

            ans += src[i][j]

print(ans)
N = int(eval(input()))
src = [list(map(int, input().split())) for i in range(N)]
ans = 0
for k in range(N):
    for i in range(N):
        for j in range(N):
            if src[i][j] > src[i][k] + src[k][j]:
                print((-1))
                exit()
            src[i][j] = min(src[i][j], src[i][k] + src[k][j])
for i in range(N - 1):
    for j in range(i + 1, N):
        use = True
        for k in range(N):
            if k == i or k == j:
                continue
            if src[i][j] == src[i][k] + src[k][j]:
                use = False
                break
        if use:
            ans += src[i][j]
print(ans)

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

-            src[i][j] = min(src[i][j], src[i][k] + src[k][j])