void main(){
  import std.stdio, std.string, std.conv, std.algorithm;
  import std.math;
  
  int n; rd(n);
  auto a=new long[][](n, n);
  foreach(i; 0..n) a[i]=readln.split.to!(long[]);

  auto d=new long[][](n, n);
  const long inf=1_000_000_000;
  foreach(i; 0..n)foreach(j; 0..n) d[i][j]=a[i][j];
  foreach(_; 0..n)foreach(i; 0..n)foreach(j; 0..n){
    d[i][j]=min(d[i][j], d[i][_]+d[_][j]);
  }
  foreach(i; 0..n)foreach(j; 0..n){
    if(d[i][j]<a[i][j]){
      writeln("-1"); return;
    }
  }
  foreach(i; 0..n)foreach(j; 0..i)foreach(k; 0..n){
    if(i!=k && j!=k && a[i][j]==a[i][k]+a[k][j]){
      a[i][j]=a[j][i]=inf;
    }
  }
  long sm=0;
  foreach(i; 0..n)foreach(j; 0..i){
    if(a[i][j]!=inf) sm+=a[i][j];
  }

  writeln(sm);

}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  foreach(i, ref e; x){
    e=l[i].to!(typeof(e));
  }
}