#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including seg_tree_order_statistics_node_update
#include <stdio.h>
#include <cassert>
using namespace std;
//using namespace __gnu_pbds;
typedef long long  lo;
typedef pair<lo,lo> ll;//pair
typedef vector<lo> vl;  //vector of long
typedef vector<ll > vll;   //vector of pair
typedef priority_queue<lo>p_q;
typedef vector< vl > vvl;  //vector of vectors
#define X first
#define Y second
#define mp(a,b) make_pair((a),(b))
#define REP(a,b) for(lo i=(a);i<(lo)b;i++)//no need to declare variable i
#define REPE(a,b,c,d) REP(a,b)for(lo j=(c);j<(lo)d;j++)//no need to declare vaiables i,j
#define REPV(a,b,c) for(lo(a)=b;(a)<(c);(a)++)//a is the variable
#define IREP(a,b) for(lo i=(a);i>=(b);i--)
#define IREPV(a,b,c) for(lo(a)=b;(a)>=(c);(a)--)
#define all(v) (v).begin(),(v).end()
#define TRV(a) for(auto it : a)
#define INF 300100
#define MOD 1000000007
#define M 1000000007
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
#define pb(a) push_back((a))
#define endl "\n"
#define eps 1e-9
#define PI 3.141592653589793
template<typename T> ostream& operator<< ( ostream &o,vector<T> v ) {
    if ( v.size() >0 )o<<v[0];
    for ( unsigned   i=1; i<v.size(); i++ )o<<" "<<v[i];
    return o<<endl;
}
template<typename U,typename V> ostream& operator<< ( ostream &o,pair<U,V> p ) {
    return o<<"("<<p.first<<", "<<p.second<<") ";
}
template<typename T> istream& operator>> ( istream &in,vector<T> &v ) {
    for ( unsigned   i=0; i<v.size(); i++ )in>>v[i];
    return in;
}
template<typename T> istream& operator>> ( istream &in,pair <T,T> &p ) {
    in>>p.X;
    in>>p.Y;
    return in;
}
typedef complex<double> base;
void fft(vector<base> & a, bool invert) {
	lo n = (lo)a.size();
	for (lo i = 1, j = 0; i<n; ++i) {
		lo bit = n >> 1;
		for (; j >= bit; bit >>= 1)j -= bit;
		j += bit;
		if (i < j) swap(a[i], a[j]);
	}
	for (lo len = 2; len <= n; len <<= 1) {
		double ang = 2 * PI / len * (invert ? -1 : 1);
		base wlen(cos(ang), sin(ang));
		for (lo i = 0; i<n; i += len) {
			base w(1);
			for (lo j = 0; j<len / 2; ++j) {
				base u = a[i + j], v = a[i + j + len / 2] * w;
				a[i + j] = u + v;
				a[i + j + len / 2] = u - v;
				w *= wlen;
			}
		}
	}
	if (invert)for (lo i = 0; i<n; ++i)a[i] /= n;
}

vl mult(vl a,vl  b) {
	//cout << a.size() << "%" << b.size() << endl;
	vector<base> fa(a.begin(), a.end()), fb(b.begin(), b.end());
	size_t n = 1;
	while (n < max(a.size(), b.size()))  n <<= 1;
	n <<= 1;
	//cout << a.size() << " " << b.size() << endl;
	fa.resize(n), fb.resize(n);
	fft(fa, false), fft(fb, false);
	for (size_t i = 0; i<n; ++i)fa[i] *= fb[i];
	fft(fa, true);
	vl res;
	res.resize(n);
	for (size_t i = 0; i<n; ++i)res[i] = int(fa[i].real() + 0.5);
	return res;
}
vl power(vl base ,lo exp){
	vl res(1024,0);
	res[0]=1;
    while(exp>0){
       if(exp%2==1)res=mult(res,base);
       while(res.size()>(1<<20))res.pop_back();
       base=mult(base,base);
       while(base.size()>(1<<20))base.pop_back();
       exp/=2;
    }
    while(res.size()>(1<<20))res.pop_back();
    return res;
}

int main(){
    std::ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);cout.precision(20);
	lo n;
	cin>>n;
	vvl AdjMatrix(n,vl(n));
	vvl b(n,vl(n,-1));
	cin>>AdjMatrix;
	bool ans=true;
	REPV (k, 0, n) // recall that #define REP(i, a, b) for (int i = int(a); i <= int(b); i++)
        REPV (i, 0, n )
            REPV (j, 0, n){
                if(i!=j and i!=k and j!=k ){
                    //cout<<i<<" "<<j<<" "<<k<<" "<<AdjMatrix[i][j]<<" "<<AdjMatrix[i][k]<<" "<<AdjMatrix[k][j]<<endl;
                    if(AdjMatrix[i][j]>AdjMatrix[i][k] + AdjMatrix[k][j])ans=false;
                    else if(AdjMatrix[i][j] == AdjMatrix[i][k] + AdjMatrix[k][j])b[i][j]=0;
                }
            }
    if(!ans)cout<<"-1";
    else {
        lo res=0;
        REPE(0,n,i,n)if(b[i][j]!=0)res+=AdjMatrix[i][j];
        cout<<res;
    }
	return 0;
}


Measured memory_bytes: 1664