import sys

from itertools import permutations, combinations

  

def solve():

    input = sys.stdin.readline

    N = int(eval(input()))

    A = [[int(a) for a in input().split()] for _ in range(N)]



    length = 0

    possible = True

    for a in range(N):

        for b, l in enumerate(A[a]):

            for k in range(N):

                if k != a and k != b:

                    if l >  A[a][k] + A[k][b]:

                        possible = False

                        break

                    elif l ==  A[a][k] + A[k][b]:

                        break

            else: length += l

        if not possible:

            print((-1))

            break

    else: print((length // 2))

    

    return 0

  

if __name__ == "__main__":

    solve()

import sys

from itertools import permutations, combinations

  

def solve():

    input = sys.stdin.readline

    N = int(eval(input()))

    A = [[int(a) for a in input().split()] for _ in range(N)]



    length = 0

    possible = True

    for a, b in combinations(list(range(N)), 2):

        Lab = A[a][b]

        for k in range(N):

            if k != a and k != b:

                if Lab >  A[a][k] + A[k][b]:

                    possible = False

                    break

                elif Lab ==  A[a][k] + A[k][b]:

                    break

        else: length += Lab

        if not possible:

            print((-1))

            break

    else: print(length)

    

    return 0

  

if __name__ == "__main__":

    solve()
import sys
from itertools import permutations, combinations


def solve():
    input = sys.stdin.readline
    N = int(eval(input()))
    A = [[int(a) for a in input().split()] for _ in range(N)]
    length = 0
    possible = True
    for a in range(N):
        for b, l in enumerate(A[a]):
            for k in range(N):
                if k != a and k != b:
                    if l > A[a][k] + A[k][b]:
                        possible = False
                        break
                    elif l == A[a][k] + A[k][b]:
                        break
            else:
                length += l
        if not possible:
            print((-1))
            break
    else:
        print((length // 2))
    return 0


if __name__ == "__main__":
    solve()

import sys
from itertools import permutations, combinations


def solve():
    input = sys.stdin.readline
    N = int(eval(input()))
    A = [[int(a) for a in input().split()] for _ in range(N)]
    length = 0
    possible = True
    for a, b in combinations(list(range(N)), 2):
        Lab = A[a][b]
        for k in range(N):
            if k != a and k != b:
                if Lab > A[a][k] + A[k][b]:
                    possible = False
                    break
                elif Lab == A[a][k] + A[k][b]:
                    break
        else:
            length += Lab
        if not possible:
            print((-1))
            break
    else:
        print(length)
    return 0


if __name__ == "__main__":
    solve()

-    for a in range(N): -        for b, l in enumerate(A[a]): -            for k in range(N): -                if k != a and k != b: -                    if l > A[a][k] + A[k][b]: -                        possible = False -                        break -                    elif l == A[a][k] + A[k][b]: -                        break -            else: -                length += l +    for a, b in combinations(list(range(N)), 2): +        Lab = A[a][b] +        for k in range(N): +            if k != a and k != b: +                if Lab > A[a][k] + A[k][b]: +                    possible = False +                    break +                elif Lab == A[a][k] + A[k][b]: +                    break +        else: +            length += Lab -        print((length // 2)) +        print(length)