#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>

#define MOD 1000000007
#define int long long
//#define PI 3.14159265358979

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

template < typename T >
ostream &operator<<(ostream &os, const vector< T > &A) {
	for (int i = 0; i < A.size(); i++)
		os << A[i] << " ";
	os << endl;
	return os;
}
template <>
ostream &operator<<(ostream &os, const vector< vector< int > > &A) {
	int N = A.size();
	int M;
	if (N > 0)
		M = A[0].size();
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++)
			os << A[i][j] << " ";
		os << endl;
	}
	return os;
}

typedef pair< int, int > pii;
typedef long long ll;

struct edge {
	int from, to, d, c;
	edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) {
		from = _from;
		to = _to;
		d = _d;
		c = _c;
	}
	bool operator<(const edge &rhs) const {
		return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d);
	}
};

typedef vector< edge > edges;
typedef vector< edges > graph;
struct flow {
	int to, cap, rev, cost;
	flow(int to = 0, int cap = 0, int rev = 0, int cost = 0) : to(to), cap(cap), rev(rev), cost(cost) {}
};
typedef vector< vector< flow > > flows;

const int di[4] = {0, -1, 0, 1};
const int dj[4] = {-1, 0, 1, 0};
const int ci[5] = {0, 0, -1, 0, 1};
const int cj[5] = {0, -1, 0, 1, 0};
const ll LINF = LLONG_MAX / 2;
const int INF = INT_MAX / 2;
const double PI = acos(-1);

template < typename T, typename U >
bool chmin(T &x, const U &y) {
	if (x > y) {
		x = y;
		return true;
	}
	return false;
}
template < typename T, typename U >
bool chmax(T &x, const U &y) {
	if (x < y) {
		x = y;
		return true;
	}
	return false;
}

struct initializer {
	initializer() {
		cout << fixed << setprecision(11);
	}
};
initializer _____;
struct UFT {
	vector< int > par;
	int getpar(int i) {
		if (par[i] == i)
			return i;
		return par[i] = getpar(par[i]);
	}
	void unite(int i, int j) {
		int t = getpar(i);
		par[t] = getpar(j);
	}
	bool uf(int i, int j) {
		return getpar(i) == getpar(j);
	}
	UFT(int n) {
		par = vector< int >(n);
		for (int i = 0; i < n; i++)
			par[i] = i;
	}
};
int minT(edges &es, int n) {
	sort(es.begin(), es.end());
	int ret = 0;
	UFT *u = new UFT(n);
	for (int i = 0; i < es.size(); i++) {
		edge e = es[i];
		if (u->uf(e.from, e.to))
			continue;
		ret += e.d;
		u->unite(e.from, e.to);
	}
	for (int i = 1; i < n; i++)
		if (!u->uf(0, i))
			return -1;
	return ret;
}

int N, M, K, T, Q;
signed main() {
	cin >> N;
	vector< vector< int > > G(N, vector< int >(N));
	vector< vector< int > > F(N, vector< int >(N, 1));
	rep(i, N) rep(j, N) cin >> G[i][j];
	int ans = 0;
	rep(k, N) rep(i, N) rep(j, N) {
		if (G[i][k] + G[k][j] < G[i][j])
			ans = -1;
		else if (k != i && k != j && G[i][k] + G[k][j] == G[i][j])
			F[i][j] = 0;
	}
	if (ans == -1) {
		cout << -1 << endl;
	} else {
		for (int i = 0; i < N - 1; i++)
			for (int j = i + 1; j < N; j++)
				if (F[i][j])
					ans += G[i][j];

		cout << ans << endl;
	}
	return 0;
}

Measured memory_bytes: 1664