void main(){
  import std.stdio, std.string, std.conv, std.algorithm;
  import std.math, std.typecons;
  
  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 inf=to!long(1e18);
  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;
    }
  }
  auto edge=new Tuple!(long, int, int)[](0);
  foreach(i; 0..n)foreach(j; 0..i){
    edge~=tuple(a[i][j], i, j);
  }
  sort!("a[0]>b[0]")(edge);
  foreach(e; edge)foreach(k; 0..n){
    if(e[1]!=k && e[2]!=k && e[0]==a[e[1]][k]+a[k][e[2]]){
      a[e[1]][e[2]]=a[e[2]][e[1]]=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));
  }
}