object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val n = sc.nextInt()
  val a = Array.fill(n, n)(sc.nextLong())

  var possible = true
  var ans = 0L
  for {
    x <- 0 until n
    y <- x+1 until n
  } {
    val d2 = a(x)(y)
    var necessary = true
    for (z <- 0 until n if necessary && z != x && z != y) {
      val d1 = a(x)(z) + a(z)(y)
      if (d1 < d2) {
        possible = false
      } else if (d1 == d2) {
        necessary = false
      }
    }
    if (necessary) {
      ans += d2
    }
  }

  println(if (possible) ans else -1)
}
