#include "bits/stdc++.h"
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL<<60;
const long long int Mod = 1000000007;
using ll = long long int; using ci = const int;
using vi = vector<int>;  using Vi = vector<long long int>;
using P = pair<int, int>;  using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define rep(i,N) for(ll i = 0; i < (ll)N; i++)
#define repi(i,a,b) for(ll i = ll(a); i < ll(b); ++i)
template<class T>bool chmax(T &former, const T &b) { if (former<b) { former=b; return true; } return false; }
template<class T>bool chmin(T &former, const T &b) { if (b<former) { former=b; return true; } return false; }
template<class T>T sqar(T x){ return x*x; }//sqrt(x)は平方根;
#define Sort(v) std::sort(v.begin(), v.end(), std::greater<decltype(v[0])>()) //降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v> >
template<class T> inline void princ(T x){cout<<x<<" ";}; 
template<class T> inline void print(T x){cout<<x<<"\n";};
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
///////////////////////////////////////////////////////////////////////////////////

struct edge{/*重み付き,有向*/
    int to , cost;/*toは接続先*/
    edge(ll to_ ,ll cost_ = 1){ to=to_; cost=cost_; };/*全てのcost=1 <=> 無向グラフ*/
};
ll n;
bool operator<(edge e1 ,edge e2){return e1.cost < e2.cost; }
bool operator>(edge e1 ,edge e2){return e1.cost > e2.cost; }
using graph =  vector< vector<edge> >;
graph g;
vector<PLL> ck;
matrix warshall_floyd(graph _g){
    matrix dis(_g.size()+1,vector<ll>(_g.size()+1,INF));
    rep(i,dis.size())dis[i][i]=0;
    rep(i,_g.size()){
        rep(j,_g[i].size()){
            edge e = _g[i][j];
            dis[i][e.to] = e.cost;
        }
    }
    rep(i,_g.size()){
        rep(j,_g.size()){
            rep(k,_g.size()){
                chmin(dis[j][k],dis[j][i]+dis[i][k]);
            }
        }
    }
    return dis;
}

ll prim(graph gg){//gg/{0}のmstを求める
    Vi vert;//int the tree?
    ll lgt=0;//length
    graph mst(gg.size());//mst
    vert.pb(1);
    p_queue(edge) q;
    rep(i,gg[1].size()){
        q.push(gg[1][i]);
    }
    while(vert.size()!=gg.size()-1){
        edge id = q.top();
        q.pop();
        bool ck = true;
        rep(i,vert.size()){
            if(id.to==vert[i]){
                ck = false;
                break;
            }
        }
        if(ck){
            vert.pb(id.to);
            lgt += id.cost;
            rep(i,gg[id.to].size()){
                q.push(gg[id.to][i]);
                mst[id.to].pb(gg[id.to][i]);
            }
        }
    }
    return lgt;//mst's length
}

int main(){
    //cin.tie(0);ios::sync_with_stdio(false);
    std::cout<<std::fixed<<std::setprecision(30);
    cin>>n;
    g.resize(n+1);
    ll a;
    matrix aa(n+1,Vi(n+1,INF));
    rep(i,n){
        rep(j,n){
            cin>>a;
            aa[i+1][j+1]=a;
            g[i+1].pb(edge(j+1,a));
        }
    }
    matrix dist = warshall_floyd(g);
    bool ans = true;
    rep(i,n){
        rep(j,n){
            if(dist[i+1][j+1]!=aa[i+1][j+1]){
                ans = false;
            }
        }
    }

    if(ans){
        //print(prim(g));
        ll sum = 0;
        rep(k,g.size()){
            rep(j,g.size()){
                rep(i,g.size()){
                    if(dist[i][j] == dist[i][k] + dist[k][j]){
                        if(i!=k && j!=k){
                            ck.pb(mp(i,j));
                        }
                    }
                }
            }
        }
        matrix kk(g.size(),Vi(g.size(),0));
        rep(i,ck.size()){
            kk[ck[i].first][ck[i].second] =1;
        }
        rep(i,g.size()){
            rep(j,g.size()){
                if(kk[i][j]==1)continue;
                if(dist[i][j]==INF)continue;
                sum += dist[i][j];
            }
        }
        print(sum/2);
    }else{
        print(-1);
    }
    return 0;
}

Measured memory_bytes: 70632