// Original C++ solution (slower):
#include<iostream>

#include<algorithm>

#include<functional>

#include<string>

#include<vector>

#include<map>

#include<set>

#include<tuple>

#include<stack>

#include<queue>

#include<deque>

#include<sstream>

#include<stdio.h>

#include<math.h>

#include<stdlib.h>

#include<bitset>

#include<time.h>

#include<cstdlib>

#include<cassert>

#define ll long long

#define fi first

#define se second

using namespace std;



int n;

const ll inf=(ll)1e14;

ll dp[310][310];



bool aru[310][310];

                   

int main(){

  ios::sync_with_stdio(false);

  cin.tie(0);

  cout.precision(10);

  cout<<fixed;

#ifdef LOCAL_DEFINE

    freopen("in", "r", stdin); 

    freopen("out","w",stdout);

#endif

  cin>>n;

  for(int i=0;i<310;i++){

    for(int j=0;j<310;j++){

      dp[i][j]=inf;

    }

  }

  for(int i=0;i<n;i++){

    for(int j=0;j<n;j++){

    cin>>dp[i][j];

    }

  }

  for(int i=0;i<n;i++){

    for(int j=0;j<n;j++){

      aru[i][j]=1;

    }

  }

  for(int k=0;k<n;k++){

    for(int i=0;i<n;i++){

      for(int j=0;j<n;j++){

        if(dp[i][j]>dp[i][k]+dp[k][j]){

          cout<<-1<<endl;

          return 0;

        }

      }

    }

  }

  ll ans=0;

  for(int i=0;i<n;i++){

    for(int j=0;j<n;j++){

      bool flag=1;

      for(int k=0;k<n;k++){

        if(k==i || k==j)continue;

        if(dp[i][j]==dp[i][k]+dp[k][j])flag=0;

      }

      if(flag)ans+=dp[i][j];

    }

  }

  cout<<ans/2<<endl;

#ifdef LOCAL_DEFINE

    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";

#endif

  return 0;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

#define fi first

#define se second

#define show(x) cerr<<#x<<"="<<x<<"\n"

typedef long long ll;

template<typename T>

void print_to(std::ostream &os,const char *,const char *tail,const T &fst){

  os<<fst<<tail;

}

template<typename Fst,typename... Rst>

void print_to(std::ostream &os,const char *del,const char *tail,const Fst &fst,const Rst &... rst){

  os<<fst<<del;

  print_to(os,del,tail,rst...);

}

template<typename Iter>

void print_to_(std::ostream &os,const char *del,const char *tail,Iter bgn,Iter end){

  for(Iter it=bgn;it!=end;){

    os<<*it;

    if(++it!=end)os<<del;

    else os<<tail;

  }

}

template<typename Fst,typename... Rst>

void println(const Fst &fst,const Rst &... rst){

  print_to(std::cout,"\n","\n",fst,rst...);

}

template<typename Fst,typename... Rst>

void print(const Fst &fst,const Rst &... rst){

  print_to(std::cout," ","\n",fst,rst...);

}

template<typename Iter>

void println_(Iter bgn,Iter end){

  print_to_(std::cout," ","\n",bgn,end);

}

using namespace std;

//const ll MOD=(ll)1e9+7;

//const ll inf=(ll)1e14;

const int dy[]={1,0,-1};

const int dx[]={1,0,-1};

int n,m,h,w;

string s;

ll dp[301][301];



int main(){

  ios::sync_with_stdio(false);

  cin.tie(0);

  cout.precision(10);

  cout<<fixed;

#ifdef LOCAL_DEFINE

  FILE *stream1;

  //FILE *stream2;

  stream1=freopen("in","r",stdin);

  //stream2=freopen("out","w",stdout);

  if(stream1==NULL)return 0;

  //if(stream2==NULL)return 0;

#endif

  cin>>n;

  for(int i=0;i<n;i++){

    for(int j=0;j<n;j++){

      cin>>dp[i][j];

    }

  }

  ll ans=0;

  for(int k=0;k<n;k++){

    for(int i=0;i<n;i++){

      for(int j=0;j<n;j++){

        if(dp[i][j]>dp[i][k]+dp[k][j]){

          print(-1);

          return 0;

        }

      }

    }

  }

  for(int i=0;i<n;i++){

    for(int j=i+1;j<n;j++){

      bool flag=0;

      for(int k=0;k<n;k++){

        if(k==i || k==j)continue;

        if(dp[i][j]==dp[i][k]+dp[k][j])flag=1;

      }

      if(!flag)ans+=dp[i][j];

    }

  }

  print(ans);

#ifdef LOCAL_DEFINE

  cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";

  fclose(stream1);

  //fclose(stream2);

#endif

  return 0;

}










// Fastest known solution for this problem: