from collections import defaultdict, deque, Counter

from heapq import heappush, heappop, heapify

import math

from bisect import bisect_left, bisect_right

import random

from itertools import permutations, accumulate, combinations

import sys

import string







INF = float('inf')

def LI(): return list(map(int, sys.stdin.readline().split()))

def I(): return int(sys.stdin.readline())

def LS(): return sys.stdin.readline().split()

def S(): return sys.stdin.readline().strip()

def IR(n): return [I() for i in range(n)]

def LIR(n): return [LI() for i in range(n)]

def SR(n): return [S() for i in range(n)]

def LSR(n): return [LS() for i in range(n)]

def SRL(n): return [list(S()) for i in range(n)]

def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]

mod = 10 ** 9 + 7









def warshall_floyd(graph):

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

    for k in range(n):

        for i in range(n):

            for j in range(n):

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

                    return 1









n = I()

graph = [[INF] * n for _ in range(n)]

#d[u][v] : 辺uvのコスト(存在しないときはinf)

for i in range(n):

    edges = LI()

    for j in range(n):

        graph[i][j] = edges[j]

        graph[j][i] = edges[j]







if warshall_floyd(graph):

    print((-1))

else:

    ret = 0

    for i in range(n):

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

            if i == j:

                continue

            for k in range(n):

                if k != i and k != j and graph[i][k] + graph[k][j] == graph[i][j]:

                        break

            else:

                ret += graph[i][j]

    print(ret)
from collections import defaultdict, deque, Counter

from heapq import heappush, heappop, heapify

import math

import bisect

import random

from itertools import permutations, accumulate, combinations, product

import sys

import string

from bisect import bisect_left, bisect_right

from math import factorial, ceil, floor, atan2

from operator import mul

from functools import reduce

sys.setrecursionlimit(10 ** 9)



INF = 10 ** 20

def LI(): return list(map(int, sys.stdin.buffer.readline().split()))

def I(): return int(sys.stdin.buffer.readline())

def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()

def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')

def IR(n): return [I() for i in range(n)]

def LIR(n): return [LI() for i in range(n)]

def SR(n): return [S() for i in range(n)]

def LSR(n): return [LS() for i in range(n)]

def SRL(n): return [list(S()) for i in range(n)]

def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]

mod = 10 ** 9 + 7





n = I()

G = []

for _ in range(n):

    G += [LI()]





ret = 0

for i in range(n):

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

        flag = 1

        for k in range(n):

            if i == k or j == k:

                continue

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

                print((-1))

                exit()

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

                flag = 0

        if flag:

            ret += G[i][j]





print(ret)
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
from bisect import bisect_left, bisect_right
import random
from itertools import permutations, accumulate, combinations
import sys
import string

INF = float("inf")


def LI():
    return list(map(int, sys.stdin.readline().split()))


def I():
    return int(sys.stdin.readline())


def LS():
    return sys.stdin.readline().split()


def S():
    return sys.stdin.readline().strip()


def IR(n):
    return [I() for i in range(n)]


def LIR(n):
    return [LI() for i in range(n)]


def SR(n):
    return [S() for i in range(n)]


def LSR(n):
    return [LS() for i in range(n)]


def SRL(n):
    return [list(S()) for i in range(n)]


def MSRL(n):
    return [[int(j) for j in list(S())] for i in range(n)]


mod = 10**9 + 7


def warshall_floyd(graph):
    # d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if graph[i][j] > graph[i][k] + graph[k][j]:
                    return 1


n = I()
graph = [[INF] * n for _ in range(n)]
# d[u][v] : 辺uvのコスト(存在しないときはinf)
for i in range(n):
    edges = LI()
    for j in range(n):
        graph[i][j] = edges[j]
        graph[j][i] = edges[j]
if warshall_floyd(graph):
    print((-1))
else:
    ret = 0
    for i in range(n):
        for j in range(i + 1, n):
            if i == j:
                continue
            for k in range(n):
                if k != i and k != j and graph[i][k] + graph[k][j] == graph[i][j]:
                    break
            else:
                ret += graph[i][j]
    print(ret)

from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, atan2
from operator import mul
from functools import reduce

sys.setrecursionlimit(10**9)
INF = 10**20


def LI():
    return list(map(int, sys.stdin.buffer.readline().split()))


def I():
    return int(sys.stdin.buffer.readline())


def LS():
    return sys.stdin.buffer.readline().rstrip().decode("utf-8").split()


def S():
    return sys.stdin.buffer.readline().rstrip().decode("utf-8")


def IR(n):
    return [I() for i in range(n)]


def LIR(n):
    return [LI() for i in range(n)]


def SR(n):
    return [S() for i in range(n)]


def LSR(n):
    return [LS() for i in range(n)]


def SRL(n):
    return [list(S()) for i in range(n)]


def MSRL(n):
    return [[int(j) for j in list(S())] for i in range(n)]


mod = 10**9 + 7
n = I()
G = []
for _ in range(n):
    G += [LI()]
ret = 0
for i in range(n):
    for j in range(i + 1, n):
        flag = 1
        for k in range(n):
            if i == k or j == k:
                continue
            if G[i][j] > G[i][k] + G[k][j]:
                print((-1))
                exit()
            elif G[i][j] == G[i][k] + G[k][j]:
                flag = 0
        if flag:
            ret += G[i][j]
print(ret)

-from bisect import bisect_left, bisect_right +import bisect -from itertools import permutations, accumulate, combinations +from itertools import permutations, accumulate, combinations, product +from bisect import bisect_left, bisect_right +from math import factorial, ceil, floor, atan2 +from operator import mul +from functools import reduce -INF = float("inf") +sys.setrecursionlimit(10**9) +INF = 10**20 -    return list(map(int, sys.stdin.readline().split())) +    return list(map(int, sys.stdin.buffer.readline().split())) -    return int(sys.stdin.readline()) +    return int(sys.stdin.buffer.readline()) -    return sys.stdin.readline().split() +    return sys.stdin.buffer.readline().rstrip().decode("utf-8").split() -    return sys.stdin.readline().strip() +    return sys.stdin.buffer.readline().rstrip().decode("utf-8") - - -def warshall_floyd(graph): -    # d[i][j]: iからjへの最短距離 -    for k in range(n): -        for i in range(n): -            for j in range(n): -                if graph[i][j] > graph[i][k] + graph[k][j]: -                    return 1 - - -graph = [[INF] * n for _ in range(n)] -# d[u][v] : 辺uvのコスト(存在しないときはinf) +G = [] +for _ in range(n): +    G += [LI()] +ret = 0 -    edges = LI() -    for j in range(n): -        graph[i][j] = edges[j] -        graph[j][i] = edges[j] -if warshall_floyd(graph): -    print((-1)) -else: -    ret = 0 -    for i in range(n): -        for j in range(i + 1, n): -            if i == j: +    for j in range(i + 1, n): +        flag = 1 +        for k in range(n): +            if i == k or j == k: -            for k in range(n): -                if k != i and k != j and graph[i][k] + graph[k][j] == graph[i][j]: -                    break -            else: -                ret += graph[i][j] -    print(ret) +            if G[i][j] > G[i][k] + G[k][j]: +                print((-1)) +                exit() +            elif G[i][j] == G[i][k] + G[k][j]: +                flag = 0 +        if flag: +            ret += G[i][j] +print(ret)