import sys

read = sys.stdin.read

readline = sys.stdin.readline

readlines = sys.stdin.readlines



def main():

    N = int(readline())

    A = [[int(i) for i in readline().split()] for _ in range(N)]



    ans = 0

    for i in range(N-1):

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

            d = A[i][j]

            flag = True

            for k in range(N):

                if k == i or k == j:

                    continue

                

                tmp = A[i][k] + A[k][j]

                if tmp < d:

                    print((-1))

                    exit()

                

                elif tmp == d:

                    flag = False

                    break

            if flag:

                ans += d

    

    print(ans)





if __name__ == "__main__":

    main()

import numpy as np

import sys

read = sys.stdin.read

readline = sys.stdin.readline

readlines = sys.stdin.readlines



def main():

    N = int(readline())

    A = np.array(read().split(), dtype=np.int64).reshape(N,N)



    INF = 10**15

    ans = 0

    for i in range(N):

        B = A[i,:] + A

        np.fill_diagonal(B, INF)

        B[:,i] = INF

        C = B.min(axis=1)

        C[i] = 0



        if (C < A[i,:]).sum() > 0:

            print((-1))

            exit()

        else:

            index = C > A[i,:]

            ans += A[i, index].sum()



    print((ans // 2))





if __name__ == "__main__":

    main()

import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines


def main():
    N = int(readline())
    A = [[int(i) for i in readline().split()] for _ in range(N)]
    ans = 0
    for i in range(N - 1):
        for j in range(i + 1, N):
            d = A[i][j]
            flag = True
            for k in range(N):
                if k == i or k == j:
                    continue
                tmp = A[i][k] + A[k][j]
                if tmp < d:
                    print((-1))
                    exit()
                elif tmp == d:
                    flag = False
                    break
            if flag:
                ans += d
    print(ans)


if __name__ == "__main__":
    main()

import numpy as np
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines


def main():
    N = int(readline())
    A = np.array(read().split(), dtype=np.int64).reshape(N, N)
    INF = 10**15
    ans = 0
    for i in range(N):
        B = A[i, :] + A
        np.fill_diagonal(B, INF)
        B[:, i] = INF
        C = B.min(axis=1)
        C[i] = 0
        if (C < A[i, :]).sum() > 0:
            print((-1))
            exit()
        else:
            index = C > A[i, :]
            ans += A[i, index].sum()
    print((ans // 2))


if __name__ == "__main__":
    main()

+import numpy as np -    A = [[int(i) for i in readline().split()] for _ in range(N)] +    A = np.array(read().split(), dtype=np.int64).reshape(N, N) +    INF = 10**15 -    for i in range(N - 1): -        for j in range(i + 1, N): -            d = A[i][j] -            flag = True -            for k in range(N): -                if k == i or k == j: -                    continue -                tmp = A[i][k] + A[k][j] -                if tmp < d: -                    print((-1)) -                    exit() -                elif tmp == d: -                    flag = False -                    break -            if flag: -                ans += d -    print(ans) +    for i in range(N): +        B = A[i, :] + A +        np.fill_diagonal(B, INF) +        B[:, i] = INF +        C = B.min(axis=1) +        C[i] = 0 +        if (C < A[i, :]).sum() > 0: +            print((-1)) +            exit() +        else: +            index = C > A[i, :] +            ans += A[i, index].sum() +    print((ans // 2))