import std.algorithm;
import std.array;
import std.ascii;
import std.container;
import std.conv;
import std.format;
import std.math;
import std.range;
import std.stdio;
import std.string;
import std.typecons;

void main()
{
  int n = readln.chomp.to!int;
  auto a = new long[][](n);
  foreach (i; 0 .. n)
  {
    a[i] = readln.chomp.split.map!(to!long).array;
  }
  long sum = 0;
  bool yes = true;
  foreach (i; 0 .. n)
  {
    foreach (j; i + 1 .. n)
    {
      bool ok = true;
      foreach (k; 0 .. n)
      {
        if (k == i || k == j)
        {
          continue;
        }
        yes &= a[i][j] <= a[i][k] + a[k][j];
        ok &= a[i][j] < a[i][k] + a[k][j];
      }
      if (ok) {
        sum += a[i][j];
      }
    }
  }
  if (yes)
    sum.writeln;
  else
    writeln = -1;
}
