// Try AtCoder
// author: Leonardone @ NEETSDKASU
import std.stdio      : readln, writeln;
import std.array      : split, appender;
import std.string     : chomp;
import std.conv       : to;
import std.algorithm  : min;

auto gets() { return readln.chomp; }
auto getVals(T)() { return gets.split.to!(T[]); }

void main() {
    auto n = gets.to!int;
    auto ap = appender!(int[][]);
    for (int i = 0; i < n; i++) {
        ap.put(getVals!int);
    }
    auto table = ap.data;
    for (int k = 0; k < n; k++) {
        for (int i = 0; i < n; i++) {
            if (i == k) { continue; }
            for (int j = 0; j < n; j++) {
                if (j == k) { continue; }
                if (i == j) { continue; }
                if (table[i][j] > table[i][k] + table[k][j]) {
                    writeln(-1);
                    return;
                }
            }
        }
    }
    auto ans = 0;
    auto xs = new int[n];
    xs[0] = -1;
    for (int i = 1; i < n; i++) {
        xs[i] = table[0][i];
    }
    for (;;) {
        int minx = 1000000007, p = 0;
        for (int i = 0; i < n; i++) {
            if (xs[i] < 0) { continue; }
            if (xs[i] < minx) {
                minx = xs[i];
                p = i;
            }
        }
        if (p == 0) {
            break;
        }
        ans += minx;
        xs[p] = -1;
        for (int i = 0; i < n; i++) {
            if (xs[i] < 0) { continue; }
            xs[i] = min(xs[i], table[p][i]);
        }
    }
    writeln(ans);
}