import java.lang.Math

fun main(args: Array<String>) {
    val INF = 10e15.toLong()
    val N = readInputLine().toInt()
    val AArr = Array(N) { readInputLine().split(" ").map { it.toLong() }.toLongArray() }
    val dist = Array(N) { AArr[it].copyOf() }

    for (k in 0 until N) {
        for (i in 0 until N) {
            for (j in 0 until N) {
                dist[i][j] = Math.min(dist[i][j], dist[i][k] + dist[k][j])
            }
        }
    }

    var ans = 0L

    for (i in 0 until N) {
        for (j in i + 1 until N) {
            var addFlag = true
            if (dist[i][j] < AArr[i][j]) {
                println(-1)
                return
            }
            for (k in 0 until N) {
                if (k == i || k == j) {
                    continue
                }
                if (dist[i][j] == dist[i][k] + dist[k][j]) {
                    addFlag = false
                    break
                }
            }
            if (addFlag) {
                ans += dist[i][j]
            }
        }
    }

    println(ans)
}

fun readInputLine(): String {
    return readLine()!!
}
