#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;


int main()
{
  int n; cin >> n;
  vector<vector<int>> a(n, vector<int>(n));
  rep(i, n)
    rep(j, n)
      cin >> a[i][j];
  rep(k, n)
    rep(i, n)
      rep(j, n)
        if (a[i][j] > a[i][k] + a[k][j])
        {
          printf("-1\n");
          return 0;
        }
  ll ans = 0;
  for (int i=0; i<n-1; i++)
    for (int j=i+1; j<n; j++)
    {
      bool check = true;
      rep(k, n)
        if (k != i && k != j && a[i][j] == a[i][k] + a[k][j])
          check = false;
      if (check)
        ans += a[i][j];
    }
  cout << ans << endl;
}