#include <bits/stdc++.h>
#define int long long
#define double long double
#define rep(i, a, n) for(int i = (int)(a);(i) < (int)(n);++(i))
#define repe(i, a, n) for(int i = (int)(a);(i) <= (int)(n);++(i))
#define repif(i, a, x) for(int i = (int)(a);(x);++(i))
#define repr(i, a, n) for(int i = ((int)(a)-1);(i) >= (int)(n);--(i))
#define reper(i, a, n) for(int i = (int)(a);(i) >= (int)(n);--(i))
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define CEIL(x) ((int)ceil(((double)x)))
#define POW(x, y) ((int)pow(x, y))
#define UNIQUE(x) (x).erase(unique(ALL((x))), end(x))
#define gcd(x, y) __gcd(x, y)
#define lcm(x, y) ((x)/gcd(x,y)*(y))
#define isin(x, l, r) ((l) <= (x) and (x) < (r))
#define fcout(n) cout << fixed << setprecision(n)
#define rcout(n) cout << right << setw(n)
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define PI (acosl(-1))
#define EPS (1e-10)
#define INF LLONG_MAX
#define INF32 INT32_MAX
#define MOD 1000000007LL
using namespace std;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using PII = pair<int, int>;
using TIII = tuple<int, int, int>;
using VPII = vector<pair<int, int>>;
using VTIII = vector<tuple<int, int, int>>;
using Complex = complex<double>;
template<typename T, template<typename> typename C = less> using PQ = priority_queue<T, vector<T>, C<T>>;
template<typename T> inline bool chmax(T& a, const T& b, bool t = false){ if(a<b or (t and a==b)){ a=b; return true; } return false; }
template<typename T> inline bool chmin(T& a, const T& b, bool t = false){ if(a>b or (t and a==b)){ a=b; return true; } return false; }


template<typename T>
struct WarshallFloyd {
    vector<vector<T>> ds;
    vector<vector<int>> next;
    WarshallFloyd() = default;
    WarshallFloyd(int n) : ds(n, vector<T>(n, numeric_limits<T>::max())), next(n, vector<int>(n)){
        for(int i = 0;i < n;++i) ds[i][i] = 0;
        for(int i = 0;i < n;++i) for(int j = 0;j < n;++j) next[i][j] = j;
    }
    void addEdge(int u, int v, T c){
        ds[u][v] = c;
    }
    void build(){
        int n = ds.size();
        for(int k = 0;k < n;++k) for(int i = 0;i < n;++i){
            if(ds[i][k] == numeric_limits<T>::max()) continue;
            for(int j = 0;j < n;++j){
                if(ds[k][j] == numeric_limits<T>::max()) continue;
                if(ds[i][j] > ds[i][k]+ds[k][j]) ds[i][j] = ds[i][k]+ds[k][j], next[i][j] = next[i][k];
            }
        }
    }
    const vector<T>& operator[](int n) const { return ds[n]; }
    vector<int> restore(int from, int to){
        vector<int> res;
        while(from != to) res.push_back(from), from = next[from][to];
        res.push_back(to);
        return res;
    }
    bool negativeCycle(){
        int n = ds.size();
        for(int i = 0;i < n;++i) if(ds[i][i] < 0) return true;
        return false;
    }
};


int N;
int A[310][310];
WarshallFloyd<int> G;


signed main(){
    cin >> N;
    G = WarshallFloyd<int>(N);
    rep(i, 0, N) rep(j, 0, N){
        cin >> A[i][j];
        G.addEdge(i, j, A[i][j]);
        G.addEdge(j, i, A[i][j]);
    }

    G.build();

    rep(i, 0, N) rep(j, 0, N) if(G[i][j] < A[i][j]){ cout << "-1" << endl; return 0; }

    int ans = 0;
    rep(i, 0, N) rep(j, i+1, N){
        bool f = true;
        rep(k, 0, N) if(i != k and k != j and G[i][j] == G[i][k]+G[k][j]) f = false;
        if(f) ans += G[i][j];
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 2432