#include "bits/stdc++.h"
#include <sys/timeb.h>
#include <fstream>

using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define replrev(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define reprev(i,n) replrev(i,0,n)
#define repi(itr,ds) for(auto itr=ds.begin();itr!=ds.end();itr++)
#define all(a) a.begin(),a.end()
#define mp make_pair
#define mt make_tuple
#define INF 2000000000
#define INFL 1000000000000000000LL
#define EPS (1e-8)
#define MOD 1000000007
#define PI 3.141592653589793
#define RMAX 4294967295
#define equals(a,b)(fabs((a)-(b))<EPS)

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<P> vP;
typedef vector<vector<int> > vvi;
typedef vector<vector<bool> > vvb;
typedef vector<vector<ll> > vvll;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<double> > vvd;
typedef vector<vector<P> > vvP;
typedef complex<double> comp;
typedef vector<complex<double>> vcomp;
typedef priority_queue<int, vector<int>, greater<int> > pqli;
typedef priority_queue<ll, vector<ll>, greater<ll> > pqlll;
typedef priority_queue<P, vector<P>, greater<P> > pqlP;
typedef string::const_iterator State;
class ParseError {};
struct Edge {
	int from, to, cost;
	bool operator<(Edge e) {
		return cost < e.cost;
	}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

int N;
vvi A;
Graph G;

class UnionFind {
public:
	vector<int> par, rank, _size;

	UnionFind(int n) {
		par = vector<int>(n);
		rank = vector<int>(n);
		_size = vector<int>(n);
		for (int i = 0; i < n; i++) {
			par[i] = i;
			rank[i] = 0;
			_size[i] = 1;
		}
	}

	// 木の根を求める
	int find(int x) {
		if (par[x] == x) {
			return x;
		}
		else {
			return par[x] = find(par[x]);
		}
	}

	// xとyの属する集合を併合
	void unite(int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y)return;

		if (rank[x] < rank[y]) {
			par[x] = y;
			_size[y] += _size[x];
		}
		else {
			par[y] = x;
			_size[x] += _size[y];
			if (rank[x] == rank[y])rank[x]++;
		}
	}

	// xとyが同じ集合に属するか否か
	bool same(int x, int y) {
		return find(x) == find(y);
	}

	int size(int x) {
		return _size[find(x)];
	}
};

ll Dijkstra(Graph graph, int start, vll &cost, int V, int target, ll upper) {
	pqlP Q;

	fill(cost.begin(), cost.end(), INFL);
	cost[start] = 0;

	Q.push(mp(0, start));	// (cost, index)

	while (!Q.empty()) {
		P pos = Q.top();
		Q.pop();
		if (pos.second == target) {
			//cout << "test" << pos.first << endl;
			return pos.first;
		}
		if (pos.first > upper) {
			return INFL;
		}
		rep(i, graph[pos.second].size()) {
			if (cost[graph[pos.second][i].to] > cost[pos.second] + graph[pos.second][i].cost) {
				cost[graph[pos.second][i].to] = cost[pos.second] + graph[pos.second][i].cost;
				Q.push(mp(cost[graph[pos.second][i].to], graph[pos.second][i].to));
			}
		}
	}
	return cost[target];
}

bool WarshallFloyd(vvll &graph, int V) {
	rep(k, V)rep(i, V)rep(j, V) {
		// i->(k)->j
		if (graph[i][k] != INFL && graph[k][j] != INFL) {
			graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]);
		}
	}
	rep(i, V) {
		if (graph[i][i] < 0) return false;
	}
	return true;
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> N;
	Edges edges;
	G = Graph(N);
	A = vvi(N, vi(N));
	UnionFind uf(N);

	rep(i, N) {
		rep(j, N) {
			cin >> A[i][j];
			if (i < j) {
				edges.push_back(Edge{ i,j,A[i][j] });
			}
		}
	}
	sort(all(edges));

	vvll adj(N, vll(N, INFL));
	rep(i, N)adj[i][i] = 0;
	ll ans = 0;

	for (auto edge : edges) {
		vll cost(N, INFL);
		ll dist = Dijkstra(G, edge.from, cost, N, edge.to, edge.cost);
		if (!uf.same(edge.from, edge.to) || dist > edge.cost) {
			G[edge.from].push_back(edge);
			G[edge.to].push_back(Edge{ edge.to, edge.from, edge.cost });
			adj[edge.from][edge.to] = adj[edge.to][edge.from] = edge.cost;
			ans += edge.cost;
			uf.unite(edge.from, edge.to);
		}
		else if (dist < edge.cost) {
			cout << -1 << endl;
			return 0;
		}
	}

	WarshallFloyd(adj, N);

	rep(i, N) {
		rep(j, N) {
			if (adj[i][j] != A[i][j]) {
				cout << -1 << endl;
				return 0;
			}
		}
	}

	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 1912