#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 998244353;

int main(){
  ll n;
  cin >> n;
  vector<vector<ll>>a(n,vector<ll>(n));
  for(int i=0; i<n; i++){
   for(int j=0; j<n; j++) cin >> a[i][j];
  }
  
  ll count=0;
  for(int i=0; i<n; i++){
    for(int j=i+1; j<n; j++){
      bool judge=true;
      for(int k=0; k<n; k++){
        if(k==i || k==j)continue;
        if(a[i][j]==a[i][k]+a[k][j])judge=false;
        if(a[i][j]>a[i][k]+a[k][j]){
          cout << -1 << endl;
          return 0;
        }
      }
      if(judge)count+=a[i][j];
    }
  }
  
  cout << count << endl;
  
  return 0;
  
}
  
  
  
  
