#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
int main(){
    int n;
    cin>>n;
    ll A[300][300];
    rep(i,n)rep(j,n) cin>>A[i][j];
    int z=1;
    ll ans=0;
    rep(i,n)for(int j=i+1;j<n;j++){
        int x=1;
        rep(k,n){
            if(k==i||k==j) continue;
            if(A[i][j]==A[i][k]+A[k][j]) x=0;
            if(A[i][j]>A[i][k]+A[k][j]) z=0;
        }
        if(x) ans+=A[i][j];
    }
    if(z) cout<<ans<<"\n";
    else cout<<-1<<"\n";
}