#!usr/bin/env python3

from collections import defaultdict

from collections import deque

from heapq import heappush, heappop

import sys

import math

import bisect

import random

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

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

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

def S(): return list(sys.stdin.readline())[:-1]

def IR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = I()

    return l

def LIR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = LI()

    return l

def SR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = S()

    return l

def LSR(n):

    l = [None for i in range(n)]

    for i in range(n):l[i] = SR()

    return l

mod = 1000000007



#A



#B



#C

"""

a,b,c,d,e,f = LI()

a *= 100

b *= 100

ans = [a,0]

for i in range(f//a+1):

    for j in range((f-a*i)//b+1):

        for k in range((f-a*i-b*j)//c+1):

            for l in range((f-a*i-b*j-c*k)//d+1):

                if i or j or k or l:

                    s = c*k+d*l

                    w = a*i+b*j

                    q = w*e//100

                    if s <= q:

                        if w+s <= f:

                            if ans[1]/ans[0] < s/(s+w):

                                ans = [w+s,s]

print(*ans)

"""



#D

n = I()

d = LIR(n)

l = [[1 for j in range(n)] for i in range(n)]

for k in range(n):

    for i in range(n):

        for j in range(n):

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

                print((-1))

                quit()

            if d[i][j] == d[i][k]+d[k][j]:

                l[i][j] = l[i][k]+l[k][j]



ans = 0

for i in range(n):

    for j in range(n):

        if l[i][j] == 4:

            ans += d[i][j]

ans //= 2

print(ans)

#E



#F



#G



#H



#I



#J



#K



#L



#M



#N



#O



#P



#Q



#R



#S



#T

#!usr/bin/env python3

from collections import defaultdict,deque

from heapq import heappush, heappop

import sys

import math

import bisect

import random

def LI(): return [int(x) for x in sys.stdin.readline().split()]

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

def LS():return [list(x) for x in sys.stdin.readline().split()]

def S():

    res = list(sys.stdin.readline())

    if res[-1] == "\n":

        return res[:-1]

    return res

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)]



sys.setrecursionlimit(1000000)

mod = 1000000007



def solve():

    n = I()

    d = LIR(n)

    for k in range(n):

        for i in range(n):

            for j in range(n):

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

                    print((-1))

                    return

    ans = 0

    for i in range(n):

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

            for k in range(n):

                if k in (i,j):

                    continue

                if d[i][k]+d[k][j] <= d[i][j]:

                    break

            else:                

                ans += d[i][j]

    print(ans)

    return



#Solve

if __name__ == "__main__":

    solve()

#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random


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


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


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


def S():
    return list(sys.stdin.readline())[:-1]


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


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


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


def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):
        l[i] = SR()
    return l


mod = 1000000007
# A
# B
# C
"""
a,b,c,d,e,f = LI()
a *= 100
b *= 100
ans = [a,0]
for i in range(f//a+1):
    for j in range((f-a*i)//b+1):
        for k in range((f-a*i-b*j)//c+1):
            for l in range((f-a*i-b*j-c*k)//d+1):
                if i or j or k or l:
                    s = c*k+d*l
                    w = a*i+b*j
                    q = w*e//100
                    if s <= q:
                        if w+s <= f:
                            if ans[1]/ans[0] < s/(s+w):
                                ans = [w+s,s]
print(*ans)
"""
# D
n = I()
d = LIR(n)
l = [[1 for j in range(n)] for i in range(n)]
for k in range(n):
    for i in range(n):
        for j in range(n):
            if d[i][j] > d[i][k] + d[k][j]:
                print((-1))
                quit()
            if d[i][j] == d[i][k] + d[k][j]:
                l[i][j] = l[i][k] + l[k][j]
ans = 0
for i in range(n):
    for j in range(n):
        if l[i][j] == 4:
            ans += d[i][j]
ans //= 2
print(ans)
# E
# F
# G
# H
# I
# J
# K
# L
# M
# N
# O
# P
# Q
# R
# S
# T

#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random


def LI():
    return [int(x) for x in sys.stdin.readline().split()]


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


def LS():
    return [list(x) for x in sys.stdin.readline().split()]


def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res


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)]


sys.setrecursionlimit(1000000)
mod = 1000000007


def solve():
    n = I()
    d = LIR(n)
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if d[i][k] + d[k][j] < d[i][j]:
                    print((-1))
                    return
    ans = 0
    for i in range(n):
        for j in range(i + 1, n):
            for k in range(n):
                if k in (i, j):
                    continue
                if d[i][k] + d[k][j] <= d[i][j]:
                    break
            else:
                ans += d[i][j]
    print(ans)
    return


# Solve
if __name__ == "__main__":
    solve()

-from collections import defaultdict -from collections import deque +from collections import defaultdict, deque -    return list(map(int, sys.stdin.readline().split())) +    return [int(x) for x in sys.stdin.readline().split()] -    return list(map(list, sys.stdin.readline().split())) +    return [list(x) for x in sys.stdin.readline().split()] -    return list(sys.stdin.readline())[:-1] +    res = list(sys.stdin.readline()) +    if res[-1] == "\n": +        return res[:-1] +    return res -    l = [None for i in range(n)] -    for i in range(n): -        l[i] = I() -    return l +    return [I() for i in range(n)] -    l = [None for i in range(n)] -    for i in range(n): -        l[i] = LI() -    return l +    return [LI() for i in range(n)] -    l = [None for i in range(n)] -    for i in range(n): -        l[i] = S() -    return l +    return [S() for i in range(n)] -    l = [None for i in range(n)] -    for i in range(n): -        l[i] = SR() -    return l +    return [LS() for i in range(n)] +sys.setrecursionlimit(1000000) -# A -# B -# C -""" -a,b,c,d,e,f = LI() -a *= 100 -b *= 100 -ans = [a,0] -for i in range(f//a+1): -    for j in range((f-a*i)//b+1): -        for k in range((f-a*i-b*j)//c+1): -            for l in range((f-a*i-b*j-c*k)//d+1): -                if i or j or k or l: -                    s = c*k+d*l -                    w = a*i+b*j -                    q = w*e//100 -                    if s <= q: -                        if w+s <= f: -                            if ans[1]/ans[0] < s/(s+w): -                                ans = [w+s,s] -print(*ans) -""" -# D -n = I() -d = LIR(n) -l = [[1 for j in range(n)] for i in range(n)] -for k in range(n): + + +def solve(): +    n = I() +    d = LIR(n) +    for k in range(n): +        for i in range(n): +            for j in range(n): +                if d[i][k] + d[k][j] < d[i][j]: +                    print((-1)) +                    return +    ans = 0 -        for j in range(n): -            if d[i][j] > d[i][k] + d[k][j]: -                print((-1)) -                quit() -            if d[i][j] == d[i][k] + d[k][j]: -                l[i][j] = l[i][k] + l[k][j] -ans = 0 -for i in range(n): -    for j in range(n): -        if l[i][j] == 4: -            ans += d[i][j] -ans //= 2 -print(ans) -# E -# F -# G -# H -# I -# J -# K -# L -# M -# N -# O -# P -# Q -# R -# S -# T +        for j in range(i + 1, n): +            for k in range(n): +                if k in (i, j): +                    continue +                if d[i][k] + d[k][j] <= d[i][j]: +                    break +            else: +                ans += d[i][j] +    print(ans) +    return + + +# Solve +if __name__ == "__main__": +    solve()