#include"bits/stdc++.h"

using namespace std; 

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VPLL;
typedef priority_queue<int> PQ_DESC;
typedef priority_queue<int, vector<int>, greater<int>> PQ_ASC;
typedef priority_queue<PII> PQ_DESC_PII;
typedef priority_queue<PII, vector<PII>, greater<PII>> PQ_ASC_PII;
typedef priority_queue<VL> PQ_DESC_VL;
typedef priority_queue<VL, vector<VL>, greater<VL>> PQ_ASC_VL;
typedef priority_queue<PLL> PQ_DESC_PLL;
typedef priority_queue<PLL, vector<PLL>, greater<PLL>> PQ_ASC_PLL;

#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define MP make_pair
#define SORT_ASC(c) sort(ALL(c))
//#define SORT_DESC(c) sort(ALL(c), greater<typeof(*((c).begin()))>())
#define SORT_DESC(c) sort((c).rbegin(),(c).rend())
#define REV(c) reverse((c).begin(), (c).end())
#define SIZE(a) int((a).size())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define ROF(i,a,b) for(int i=(b-1);i>=(a);--i)
#define REP(i,n) FOR(i,0,n)
#define PER(i,n) ROF(i,0,n)

const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int LARGE_INT = 1e9+100;
const int INF = 2e9+100;
const LL INF_LL = (LL)INF*(LL)INF;
const int MOD = 1e9+7;

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

// ワーシャルフロイド法(全点間距離)
VVL d;//対角0,辺がないところはINF
void warshall_floyd(int n) {
  REP(k,n){       // 経由する頂点
    REP(i,n){    // 始点
      REP(j,n){  // 終点
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
}

void Main()
{
    int n;cin>>n;
    d.resize(n);
    VVL a(n);
    VVL b;
    REP(i,n){
        d[i].resize(n);
        a[i].resize(n);
        REP(j,n){
            LL t;cin>>t;
            d[i][j] = t;
            a[i][j] = t;
            if(i<j){
                b.PB({t,i,j});
            }
        }
    }

    warshall_floyd(n);
    if(a != d){
        cout<<-1<<endl;
        return;
    }

    SORT_ASC(b);
    //VVL dd(n);
    //REP(i,n){
    //    dd[i].resize(n,INF_LL);
    //}

    LL result = 0;
    for(auto x : b){
        LL dist = x[0];
        LL now = x[1];
        LL next = x[2];
        //cerr<<dist<<" "<<now<<" "<<next<<endl;

        bool f = true;
        //if(dd[now][next] == dist){
        //    f = false;
        //}
        REP(i,n){ 
            if(now == i){
                continue;
            }
            if(next == i){
                continue;
            }
            if(d[now][i] + d[i][next] == dist){
                f = false;
                break;
            }            
            //if(dd[now][i] + dd[i][next] == dist){
            //    f = false;
            //    break;
            //}
            //if(dd[next][i] + dd[i][now] == dist){
            //    f = false;
            //    break;
            //}
        }

        if(f){
            result += dist;
            //dd[now][next] = dist;
            //dd[next][now] = dist;
            //dump(result)
        }
    }

    cout<<result<<endl;

    
	return;
}

int main()
{
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	Main();
    return 0;
}

Measured memory_bytes: 4148