#include <bits/stdc++.h>
using namespace std;
#define rep(i,x) for(int i = 0; i <x; i++)
typedef long long ll;

const ll INF = 1LL<<60;

int main(){
    int N;
    cin >> N;
    ll A[N][N];
    rep(i,N)rep(j,N) cin >> A[i][j];

    vector<vector<bool>>mat(N,vector<bool>(N,true));

    rep(k,N)rep(i,N)rep(j,N){
      if(A[i][j] > A[i][k] + A[k][j]){
        cout << -1 << endl;
        return 0;
      }else if(k!=i && k!=j && A[i][j] == A[i][k] + A[k][j]){
        mat[i][j] = false;
      }
    }
    ll ans = 0;

    rep(i,N) rep(j,N){
      if(!mat[i][j]) continue;
      //cout << i << j << endl;
       ans += A[i][j];
    }
    cout << ans/2 << endl;
}
