

def solve():

  n  = int(eval(input()))

  a = []

  for i in range(n):

    tp = list(int(i) for i in input().split())

    a.append(tp)

  a2 = []

  #warshall

  for k in range(n):

    for i in range(n):

      for j in range(n):

        if not k in [i,j]:

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

          #距離短縮可能

           print((-1))

           exit()

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

          a2.append((i,j))

  for i,j in a2:

    a[i][j] = 0

  ans = 0

  for i in range(n):

    for j in range(n):

      if i < j:

        ans += a[i][j]

  print(ans)

solve()
import sys

input = sys.stdin.readline

def solve():

  n  = int(eval(input()))

  a = [list(int(i) for i in input().split()) for _ in range(n)]

  a2 = []

  #warshall

  for k in range(n):

    for i in range(n):

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

        if not k in [i,j]:

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

          #距離短縮可能

           print((-1))

           exit()

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

          a2.append((i,j))

  for i,j in a2:

    a[i][j] = 0

  ans = 0

  for i in range(n):

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

        ans += a[i][j]

  print(ans)

solve()
def solve():
    n = int(eval(input()))
    a = []
    for i in range(n):
        tp = list(int(i) for i in input().split())
        a.append(tp)
    a2 = []
    # warshall
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if not k in [i, j]:
                    if a[i][j] > a[i][k] + a[k][j]:
                        # 距離短縮可能
                        print((-1))
                        exit()
                    elif a[i][j] == a[i][k] + a[k][j]:
                        a2.append((i, j))
    for i, j in a2:
        a[i][j] = 0
    ans = 0
    for i in range(n):
        for j in range(n):
            if i < j:
                ans += a[i][j]
    print(ans)


solve()

import sys

input = sys.stdin.readline


def solve():
    n = int(eval(input()))
    a = [list(int(i) for i in input().split()) for _ in range(n)]
    a2 = []
    # warshall
    for k in range(n):
        for i in range(n):
            for j in range(i + 1, n):
                if not k in [i, j]:
                    if a[i][j] > a[i][k] + a[k][j]:
                        # 距離短縮可能
                        print((-1))
                        exit()
                    elif a[i][j] == a[i][k] + a[k][j]:
                        a2.append((i, j))
    for i, j in a2:
        a[i][j] = 0
    ans = 0
    for i in range(n):
        for j in range(i + 1, n):
            ans += a[i][j]
    print(ans)


solve()

+import sys + +input = sys.stdin.readline + + -    a = [] -    for i in range(n): -        tp = list(int(i) for i in input().split()) -        a.append(tp) +    a = [list(int(i) for i in input().split()) for _ in range(n)] -            for j in range(n): +            for j in range(i + 1, n): -        for j in range(n): -            if i < j: -                ans += a[i][j] +        for j in range(i + 1, n): +            ans += a[i][j]