import std.stdio, std.string, std.conv, std.algorithm;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;


void main() {
    int N;
    int[][] A;

    scan(N);
    A = new int[][](N, N);
    iota(N).each!(i => A[i] = readln.split.to!(int[]));

    long ans;

    foreach (i ; 0 .. N) {
        foreach (j ; i + 1 .. N) {
            bool flag = true;

            foreach (k ; 0 .. N) {
                if (k == i || k == j) continue;
                if (A[i][j] > A[i][k] + A[k][j]) {
                    writeln(-1);
                    return;
                }
                if (A[i][j] == A[i][k] + A[k][j]) {
                    flag = false;
                    break;
                }
            }

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

    writeln(ans);
}



void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}



void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
} 