import std;

ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "number", long, "cost");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);

void main()
{
    auto n = lread();
    auto t = new long[][](n, n);
    foreach (ref e; t)
        e = aryread();

    bool check = true;
    long s;
    WF: foreach (k; iota(n))
    {
        foreach (i; iota(n))
        {
            foreach (j; iota(n))
            {
                if (t[i][j] > t[i][k] + t[k][j])
                {
                    check = false;
                    break WF;
                }
            }
        }
    }

    foreach (i; iota(n))
    {
        foreach (j; iota(i + 1, n))
        {
            bool direct = true;
            foreach (k; iota(n))
            {
                if (i != k && j != k && t[i][j] >= t[i][k] + t[k][j])
                {
                    direct = false;
                }
            }
            if(direct)
            {
                s += t[i][j];
            }
        }
    }


    if (check)
        writeln(s);
    else
        writeln(-1);
}

void scan(TList...)(ref TList Args)
{
    auto line = readln.split();
    foreach (i, T; TList)
    {
        T val = line[i].to!(T);
        Args[i] = val;
    }
}
