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; 0..n)
      t += a[i][j];

  auto dist = a.map!(i => i.dup).array;

  foreach (k; 0..n)
    foreach (i; 0..n)
      foreach (j; 0..n)
        if (dist[i][j] > dist[i][k] + dist[k][j])
          dist[i][j] = dist[i][k] + dist[k][j];

  foreach (i; 0..n)
    if (dist[i] != a[i]) {
      writeln(-1);
      return;
    }

  auto v = new bool[][](n, n);

  foreach (k; 0..n)
    foreach (i; 0..n)
      foreach (j; 0..n)
        if (i != k && j != k && !v[i][j] && a[i][j] == a[i][k] + a[k][j]) {
          t -= a[i][j];
          v[i][j] = true;
        }

  writeln(t/2);
}
