#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <algorithm>
#include <iomanip>
#include <string.h>

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define ALL(a)  (a).begin(),(a).end()

typedef long long lint;

using namespace std;

int main(){
  int N;
  cin>>N;
  lint d[N][N];
  REP(i,N)REP(j,N)cin>>d[i][j];
  lint ans=0;
  REP(i,N)FOR(j,i+1,N){
    bool f=true;
    REP(k,N){
      if(i==k or j==k)continue;
      if(d[i][j]>d[i][k]+d[k][j]){
        cout<<-1<<endl;
        return 0;
      }
      if(d[i][j]==d[i][k]+d[k][j])f=false;
    }
    if(f)ans+=d[i][j];
  }
  cout<<ans<<endl;
  return 0;
}
