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

int main() {
  int n;
  cin >> n;
  long a[n][n], s(0);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      cin >> a[i][j];
  for (int i = 0; i < n; ++i)
    for (int j = i + 1; j < n; ++j) {
      bool nf(true);
      for (int k = 0; k < n; ++k) {
        if (i == k || j == k) continue;
        if (a[i][j] > a[i][k] + a[k][j]) {
          cout << -1;
          return 0;
        }
        if (a[i][j] == a[i][k] + a[k][j]) nf = false;
        
      }
      if (nf) s += a[i][j];
    }
      
  cout << s << endl;
  return 0;
}