#include <bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e18;
#define rep(i,n) for(int i = 0;i < n;i++)

int a[300][300], G[300][300];
signed main(){
	int n;
	scanf("%lld", &n);
	rep(i,n)rep(j,n)scanf("%lld", &a[i][j]);
	rep(i,n)rep(j,n)G[i][j] = a[i][j];
	rep(i,n)rep(u,n)rep(t,n){
		G[u][t] = min(G[u][t], G[u][i]+G[i][t]);
	}
	/*rep(i,n){
		rep(j,n)printf("%d ", G[i][j]);
		printf("\n");
	}*/
	int f = 1;
	rep(i,n)rep(j,n)if(a[i][j] != G[i][j])f = 0;
	if(f == 0){
		printf("-1\n");
		return 0;
	}
	int ans = 0;
	rep(u,n)rep(t,n){
		if(u >= t)continue;
		int mn = inf;
		rep(i,n){
			if(u == i || i == t)continue;
			mn = min(mn, G[u][i]+G[i][t]);
		}
		if(G[u][t] < mn)ans += G[u][t];
	}
	printf("%lld\n", ans);
	return 0;
}