#include "bits/stdc++.h"
// Begin Header {{{
#define all(x)          (x).begin(), (x).end()
#define rep(i, n)       for (i64 i = 0, i##_limit = (n); i < i##_limit; ++i)
#define reps(i, s, t)   for (i64 i = (s), i##_limit = (t); i <= i##_limit; ++i)
#define repr(i, s, t)   for (i64 i = (s), i##_limit = (t); i >= i##_limit; --i)
#ifndef DBG
#define trace(...)
#endif
using namespace std;
using i64 = int_fast64_t;
using pii = pair<i64, i64>;
template <class T, class U> inline bool chmax(T &a, const U &b) { return b > a && (a = b, true); }
template <class T, class U> inline bool chmin(T &a, const U &b) { return b < a && (a = b, true); }
constexpr int INF  = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;

template <class T>
inline vector<T> make_v(const T &initValue, size_t sz) {
    return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto make_v(const T &initValue, size_t sz, Args... args) {
    return vector<decltype(make_v<T>(initValue, args...))>(sz, make_v<T>(initValue, args...));
}

inline void read() {}

template <class Head, class... Tail>
inline void read(Head &head, Tail&... tail) { cin >> head; read(tail...); }

inline void print() { cout << "\n"; }

template <class Head, class... Tail>
inline void print(Head &&head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail)) cout << ' ';
    print(forward<Tail>(tail)...);
}

template <class T>
inline ostream& operator<< (ostream &out, const vector<T> &vec) {
    static constexpr const char *delim[] = { " ", "" };
    for (const auto &e : vec) out << e << delim[&e == &vec.back()];
    return out;
}
// }}} End Header

template <size_t N>
class MatrixGraph { // {{{
    int64_t G[N][N];

#ifdef DBG
#define constexpr /* disable constexpr */
#endif

public:
    constexpr MatrixGraph() noexcept { init(); }

    inline constexpr void init() noexcept {
        for (int i = 0; i < N; ++i) for (int j = 0; j < N; ++j) G[i][j] = 0x3f3f3f3f3f3f3f3f;
        for (int i = 0; i < N; ++i) G[i][i] = 0;
    }

    int64_t* operator[](int i) noexcept {
        return G[i];
    }

    const int64_t* operator[] (int i) const noexcept {
        return G[i];
    }
};
// }}}

bool disable[305][305];

template<size_t N>
int hoge(MatrixGraph<N> &G, int V) // {{{
{
    for (int k = 0; k < V; ++k) {
        for (int i = 0; i < V; ++i) {
            if (G[i][k] >= LINF) continue;
            for (int j = 0; j < V; ++j) {
                if (G[k][j] >= LINF) continue;
                if (i != j && j != k && i != k) {
                    if (G[i][j] == G[i][k] + G[k][j]) {
                        disable[i][j] = disable[j][i] = true;
                    } else if (G[i][j] > G[i][k] + G[k][j]) {
                        return -1;
                    }
                }
            }
        }
    }

    return 0;
}
// }}}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);

    int N;
    read(N);

    MatrixGraph<305> G;
    rep(i, N) rep(j, N) {
        read(G[i][j]);
    }

    if (hoge(G, N) == -1) {
        print(-1);
        return 0;
    }

    i64 sum = 0;
    rep(i, N) {
        rep(j, N) {
            if (!disable[i][j]) {
                sum += G[i][j];
            }
        }
    }

    print(sum / 2);

    return 0;
}


Measured memory_bytes: 1024