# -*- coding: utf-8 -*-

from itertools import combinations



def inpl(): return tuple(map(int, input().split()))



N = int(eval(input()))

res = 0

exist = True

A = []



for _ in range(N):

    A.append(inpl())



for i, j in combinations(list(range(N)), 2):

    vervose = False



    for k in range(N):

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

            exist = False

            break

    

    if not exist:

        break

    

    for k in range(N):

        if i == k or j == k:

            pass

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

            vervose = True

            break

        

    if not vervose:

        res += A[i][j]

if exist:

    print(res)

else:

    print((-1))
# -*- coding: utf-8 -*-

from itertools import combinations

 

def inpl(): return list(map(int, input().split()))

 

N = int(eval(input()))

res = 0

exist = True

A = []

 

for _ in range(N):

    A.append(inpl())

 

for i, j in combinations(list(range(N)), 2):

    vervose = False

 

    if not exist:

        break

 

    for k in range(N):

        if i == k or j == k:

            pass

        else:

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

                pass

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

                vervose = True

            else:

                exist = False

                break

    if vervose:

        pass

    else:

        res += A[i][j]

 

if exist:

    print(res)

else:

    print((-1))
# -*- coding: utf-8 -*-
from itertools import combinations


def inpl():
    return tuple(map(int, input().split()))


N = int(eval(input()))
res = 0
exist = True
A = []
for _ in range(N):
    A.append(inpl())
for i, j in combinations(list(range(N)), 2):
    vervose = False
    for k in range(N):
        if A[i][j] > A[i][k] + A[k][j]:
            exist = False
            break
    if not exist:
        break
    for k in range(N):
        if i == k or j == k:
            pass
        elif A[i][j] == A[i][k] + A[k][j]:
            vervose = True
            break
    if not vervose:
        res += A[i][j]
if exist:
    print(res)
else:
    print((-1))

# -*- coding: utf-8 -*-
from itertools import combinations


def inpl():
    return list(map(int, input().split()))


N = int(eval(input()))
res = 0
exist = True
A = []
for _ in range(N):
    A.append(inpl())
for i, j in combinations(list(range(N)), 2):
    vervose = False
    if not exist:
        break
    for k in range(N):
        if i == k or j == k:
            pass
        else:
            if A[i][j] < A[i][k] + A[k][j]:
                pass
            elif A[i][j] == A[i][k] + A[k][j]:
                vervose = True
            else:
                exist = False
                break
    if vervose:
        pass
    else:
        res += A[i][j]
if exist:
    print(res)
else:
    print((-1))

-    return tuple(map(int, input().split())) +    return list(map(int, input().split())) -    for k in range(N): -        if A[i][j] > A[i][k] + A[k][j]: -            exist = False -            break -        elif A[i][j] == A[i][k] + A[k][j]: -            vervose = True -            break -    if not vervose: +        else: +            if A[i][j] < A[i][k] + A[k][j]: +                pass +            elif A[i][j] == A[i][k] + A[k][j]: +                vervose = True +            else: +                exist = False +                break +    if vervose: +        pass +    else: