import java.io.PrintWriter
import java.util.*
import java.math.*
import java.lang.*
import kotlin.comparisons.*

val pw = PrintWriter(System.out)
val MOD: Long = (1e+9 + 7).toLong()

fun main(args: Array<String>) {
	val n = readInt()
	val roads = Array(n) { readListOfLong().toLongArray() }
	val distances = Array(n) { Array(n) { (1e+10).toLong() } }
	for(k in 0 until n) {
		for(i in 0 until n) {
			for(j in 0 until n) {
				distances[i][j] = Math.min(distances[i][j], roads[i][j])
				distances[i][j] = Math.min(distances[i][j], distances[i][k] + distances[k][j])
			}
		}
	}

	var cnt = 0L
	var notAble = false
	for(i in 0 until n-1) {
		for(j in i+1 until n) {
			if(distances[i][j] != roads[i][j]) {
				notAble = true
				break
			}
			var found = false
			for(w in 0 until n) {
				if(w == i || w == j) continue
				if(distances[i][j] == distances[i][w] + distances[w][j]) {
					found = true
					break
				}
			}
			if(!found) {
				cnt += distances[i][j]
			}
		}
		if(notAble) {
			cnt = -1L
			break
		}
	}
	println(cnt)

	pw.flush()
}

/****** Declared Functions and Data Structures ******/
// IO
fun read() = readLine()!!
fun readInt() = read().toInt()
fun readLong() = read().toLong()
fun readDouble() = read().toDouble()
fun readListOfString() = read().split(" ")
fun readListOfInt() = readListOfString().map { it.toInt() }
fun readListOfLong() = readListOfString().map { it.toLong() }
fun readListOfDouble() = readListOfString().map { it.toDouble() }