#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = (A); I < (B); ++I)
#define FORR(I,A,B) for(ll I = (B-1); I >= (A); --I)
const ll INF=1e18+7;
const ll MOD=1e9+7;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n;
	cin >> n;
	ll d[n][n],d2[n][n],dori[n][n];
	FOR(i,0,n)FOR(j,0,n){
			cin >> d[i][j];
			dori[i][j] = d[i][j];
	}

	FOR(k,0,n)FOR(i,0,n)FOR(j,0,n){
		if(i==k||j==k)continue;
		if(d[i][j] >= d[i][k]+d[k][j]){
			d2[i][j] = d[i][j];
			d[i][j] = d[i][k]+d[k][j];
		}
	}
	ll ans = 0;
	FOR(i,0,n){
		FOR(j,0,n){
			if(d[i][j]!=dori[i][j]){
				cout << -1 << endl;
				return 0;
			}
			if(d[i][j]!=d2[i][j]){
				ans += d[i][j];
			}
		}
	}
	cout << ans/2 <<endl;
}