#include<iostream>
#include<algorithm>
using namespace std;
using ll=long long;
ll N,A[300][300],ans;
int main(){
    cin>>N;
    for(int i{};i<N;++i)
        for(int j{};j<N;++j)
            cin>>A[i][j];
    for(int i{};i<N;++i)
        for(int j{i+1};j<N;++j){
            int f{};
            for(int k{};k<N;++k){
                if(A[i][j]>A[i][k]+A[k][j]){
                    cout<<-1<<endl;
                    return 0;
                }
                if(i!=k&&j!=k&&A[i][j]==A[i][k]+A[k][j])
                    f=1;
            }
            if(!f)ans+=A[i][j];
        }
    cout<<ans<<endl;
}
