def warshall_floyd(n,d):

    #d[i][j]: iからjへの最短距離

    r = [[float("inf")]*n for i in range(n)]

    for k in range(n):

        for i in range(n):

            for j in range(n):

                r[i][j] = min(d[i][j],r[i][k] + r[k][j])

    return r



n = int(eval(input()))

a = [[] for i in range(n)]

for i in range(n):

    a[i] = list(map(int,input().split()))

for i in range(n):

    if a[i][i] != 0:

        print((-1))

        exit()





war = warshall_floyd(n,a)



res = 0

for i in range(n):

    for j in range(n):

        if war[i][j] < a[i][j]:

            print((-1))

            exit()

        elif war[i][j] == a[i][j]:

            judge = 1

            for h in range(n):

                if h != i and h != j and war[i][h]+war[h][j] == war[i][j]:

                    judge = -1

            if judge == 1:

                res += war[i][j]

print((res//2))

import copy

def warshall_floyd(d):

    #d[i][j]: iからjへの最短距離

    for k in range(n):

        for i in range(n):

            for j in range(n):

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

 

n = int(eval(input()))

a = [[] for i in range(n)]

war = [[] for i in range(n)]

for i in range(n):

    a[i] = list(map(int,input().split()))

war = copy.deepcopy(a)

for i in range(n):

    if a[i][i] != 0:

        print((-1))

        exit()

 

warshall_floyd(war)





res = 0

for i in range(n):

    for j in range(n):

        if war[i][j] < a[i][j]:

            print((-1))

            exit()

        elif war[i][j] == a[i][j]:

            judge = 1

            for h in range(n):

                if h != i and h != j and war[i][h]+war[h][j] == war[i][j]:

                    judge = -1

            if judge == 1:

                res += war[i][j]

print((res//2))
def warshall_floyd(n, d):
    # d[i][j]: iからjへの最短距離
    r = [[float("inf")] * n for i in range(n)]
    for k in range(n):
        for i in range(n):
            for j in range(n):
                r[i][j] = min(d[i][j], r[i][k] + r[k][j])
    return r


n = int(eval(input()))
a = [[] for i in range(n)]
for i in range(n):
    a[i] = list(map(int, input().split()))
for i in range(n):
    if a[i][i] != 0:
        print((-1))
        exit()
war = warshall_floyd(n, a)
res = 0
for i in range(n):
    for j in range(n):
        if war[i][j] < a[i][j]:
            print((-1))
            exit()
        elif war[i][j] == a[i][j]:
            judge = 1
            for h in range(n):
                if h != i and h != j and war[i][h] + war[h][j] == war[i][j]:
                    judge = -1
            if judge == 1:
                res += war[i][j]
print((res // 2))

import copy


def warshall_floyd(d):
    # d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j], d[i][k] + d[k][j])


n = int(eval(input()))
a = [[] for i in range(n)]
war = [[] for i in range(n)]
for i in range(n):
    a[i] = list(map(int, input().split()))
war = copy.deepcopy(a)
for i in range(n):
    if a[i][i] != 0:
        print((-1))
        exit()
warshall_floyd(war)
res = 0
for i in range(n):
    for j in range(n):
        if war[i][j] < a[i][j]:
            print((-1))
            exit()
        elif war[i][j] == a[i][j]:
            judge = 1
            for h in range(n):
                if h != i and h != j and war[i][h] + war[h][j] == war[i][j]:
                    judge = -1
            if judge == 1:
                res += war[i][j]
print((res // 2))

-def warshall_floyd(n, d): +import copy + + +def warshall_floyd(d): -    r = [[float("inf")] * n for i in range(n)] -                r[i][j] = min(d[i][j], r[i][k] + r[k][j]) -    return r +                d[i][j] = min(d[i][j], d[i][k] + d[k][j]) +war = [[] for i in range(n)] +war = copy.deepcopy(a) -war = warshall_floyd(n, a) +warshall_floyd(war)