#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <cstdio>

using namespace std;

int main(){
  int n;
  long long a[300][300] = {};
  long long b[300][300] = {};
  long long tot = 0;
  cin >> n;
  for (int i=0;i<n;i++) {
    for (int j=0;j<n;j++) {
      cin >> a[i][j];
      b[i][j] = a[i][j];
    }
  }
  for (int i=0;i<n;i++) {
    for (int j=0;j<n;j++) {
      for (int k=0;k<n;k++) {
	if (a[i][j] > a[i][k] + a[j][k]) {
	  cout << -1 << endl;
	  return 0;
	}
	if (i!=k && k!=j && a[i][j] == a[i][k] + a[j][k]) {
	  b[i][j] = 0;
	}
      }
    }
  }
  for (int i=0;i<n;i++) {
    for (int j=0;j<n;j++) {
      tot += b[i][j];
    }
  }
  cout << tot/2 << endl;
}
