#include "bits/stdc++.h"
using namespace std;

int main() {
	long long N, A[300][300] = {}, ans = 0;
	bool check = true, B[300][300] = {};
	cin >> N;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			cin >> A[i][j];
		}
	}
	for (int k = 0; k < N; k++) {
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				if (A[i][j] > A[i][k] + A[k][j]) check = false;
				if (A[i][j] == A[i][k] + A[k][j] && A[i][k] > 0 && A[k][j] > 0) B[min(i, j)][max(i, j)] = true;
			}
		}
	}
	for (int i = 0; i < N; i++) {
		for (int j = i; j < N; j++) {
			if (!B[i][j]) ans += A[i][j];
		}
	}
	if (check) cout << ans;
	else cout << -1;
}