#include <bits/stdc++.h>
#define int long long
const int maxn = 310;
using namespace std;
int G[maxn][maxn];
int n,m,i,j,k,ans;
bool acs = 1;
signed main() {
	scanf("%d",&n);
	for(int i = 1;i <= n;i++) {
		for(int j = 1;j <= n;j++) {
			scanf("%d",&G[i][j]);
		}
	}
	for(int i = 1;i <= n;i++) {
		for(int j = 1;j <= n;j++) {
			if(i == j) continue; bool flag = 1;
			for(int k = 1;k <= n;k++) {
				if(k != i && k != j) {
					if(G[i][j] >  G[i][k] + G[k][j]) acs = 0;
					if(G[i][j] == G[i][k] + G[k][j]) flag = 0;
				}
			}
			ans += flag * G[i][j];
		}
	}
	printf("%lld",acs ? ans >> 1: -1);
	return 0;
}