import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  auto n = readln.chomp.to!size_t;
  auto a = new long[][](n);

  foreach (i; 0..n) a[i] = readln.split.to!(long[]);

  auto t = 0L;
  foreach (i; 0..n)
    foreach (j; i+1..n)
      t += a[i][j];

  foreach (k; 0..n)
    foreach (i; 0..n)
      foreach (j; 0..n) {
        if (a[i][j] > a[i][k] + a[k][j]) {
          writeln(-1);
          return;
        }
        if (i > j && i != k && j != k && a[i][j] == a[i][k] + a[k][j])
          t -= a[i][j];
      }

  writeln(t);
}
