#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{BinaryHeap, HashMap};

#[allow(dead_code)]
type Graph = Vec<Vec<usize>>;

struct UnionFind {
    parent: Vec<usize>,
    size: Vec<usize>,
    rank: Vec<usize>,
}

impl UnionFind {
    pub fn new(n: usize) -> UnionFind {
        let mut parent = vec![];
        for i in 0..n {
            parent.push(i);
        }
        UnionFind {
            parent: parent,
            size: vec![1; n],
            rank: vec![0; n],
        }
    }

    pub fn root(&mut self, x: usize) -> usize {
        if self.parent[x] == x {
            x
        } else {
            let parent = self.parent[x];
            self.parent[x] = self.root(parent);
            self.parent[x]
        }
    }

    pub fn unite(&mut self, x: usize, y: usize) {
        let x_root = self.root(x);
        let y_root = self.root(y);

        if x_root == y_root {
            return;
        }

        let sum = self.size[x_root] + self.size[y_root];
        self.size[x_root] = sum;
        self.size[y_root] = sum;

        if self.rank[x_root] < self.rank[y_root] {
            self.parent[x_root] = y_root;
        } else {
            self.parent[y_root] = x_root;
            if self.rank[x_root] == self.rank[y_root] {
                self.rank[x_root] += 1;
            }
        }
    }

    pub fn same(&mut self, x: usize, y: usize) -> bool {
        self.root(x) == self.root(y)
    }

    pub fn size(&mut self, x: usize) -> usize {
        let root = self.root(x);
        self.size[root]
    }
}

fn main() {
    input! {
        n: usize,
        a: [[usize; n]; n],
    }

    let mut ans = 0;
    let mut edges = vec![];
    for i in 0..n {
        for j in 0..n {
            if a[i][j] > 0 {
                edges.push((a[i][j], i, j));
            }
        }
    }
    let mut graph = vec![vec![0; n]; n];
    let mut uf = UnionFind::new(n);
    edges.sort();

    for &(l, i, j) in &edges {
        if uf.root(i) != uf.root(j) {
            uf.unite(i, j);
            graph[i][j] = l;
            graph[j][i] = l;
            ans += l;
        }
    }

    for k in 0..n {
        for i in 0..n {
            for j in 0..n {
                if i == j {
                    continue;
                }
                if graph[i][k] > 0 && graph[k][j] > 0 {
                    let next = graph[i][k] + graph[k][j];
                    if graph[i][j] == 0 {
                        graph[i][j] = next;
                    } else {
                        graph[i][j] = min(graph[i][j], next);
                    }
                }
            }
        }
    }

    // println!("{}", ans);

    // for i in 0..n {
    //     for j in 0..n {
    //         print!("{} ", graph[i][j]);
    //     }
    //     println!("");
    // }

    for &(l, i, j) in &edges {
        if graph[i][j] < a[i][j] {
            println!("-1");
            return;
        } else if graph[i][j] > a[i][j] {
            graph[i][j] = a[i][j];
            graph[j][i] = a[i][j];
            // println!("{} {} {}", i, j, l);
            ans += l;
            for ii in 0..n {
                for jj in 0..n {
                    let next = graph[ii][i] + a[i][j] + graph[j][jj];
                    graph[ii][jj] = min(graph[ii][jj], next);
                    let next = graph[ii][j] + a[j][i] + graph[i][jj];
                    graph[ii][jj] = min(graph[ii][jj], next);
                }
            }

            // for i in 0..n {
            //     for j in 0..n {
            //         print!("{} ", graph[i][j]);
            //     }
            //     println!("");
            // }
        }
    }

    // for i in 0..n {
    //     for j in 0..n {
    //         print!("{} ", graph[i][j]);
    //     }
    //     println!("");
    // }

    println!("{}", ans);
}


Measured memory_bytes: 9956