import java.io._
import java.util.StringTokenizer
import scala.util.control.Breaks.{breakable,break}

object Main extends App {
  val in = new InputReader(System.in)
  val pw = new PrintWriter(System.out)
 
  val N = in.next().toInt
  val A = Array.ofDim[Long](N+1, N+1)
  for(y <- 1 to N)
    for(x <- 1 to N)
      A(y)(x) = in.next().toLong


  // d[a][b] は辺 e = (a, b) のコスト(存在しない場合はINF, ただし d[i][i] = 0 とする)
  val d = Array.ofDim[Long](N+1, N+1)

  // 初期化
  for(y <- 1 to N)
    for(x <- 1 to N)
      d(y)(x) = A(y)(x)

  // function
  def warshall_floyd() =
    for(k <- 1 to N; i <- 1 to N; j <- 1 to N){
      d(i)(j) = Math.min(d(i)(j), d(i)(k) + d(k)(j))
    }


  // main
  warshall_floyd()

  var flag = true
  breakable {
  for(y <- 1 to N)
    for(x <- 1 to N){
      if(d(y)(x) != A(y)(x)){
        flag = false
        break
      }
    }
  }

  val dp = Array.ofDim[Boolean](N+1, N+1)

  for(i <- 1 to N-1; j <- i+1 to N) {
    if(i != j){
      for(k <- 1 to N){
        if(d(i)(k) + d(k)(j) == A(i)(j) && k != i && k != j) {
          dp(i)(j) = true
        }
      }
    }
  }

  var ans = 0L
  for(i <- 1 to N-1; j <- i+1 to N) {
    if(!dp(i)(j))
      ans += d(i)(j)
  }

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


class InputReader(stream: InputStream) {
  private val reader = new BufferedReader(new InputStreamReader(stream))
  private var tokenizer: StringTokenizer = new StringTokenizer(reader.readLine())
  def next(): String = {
    while (!tokenizer.hasMoreTokens()) {
      tokenizer = new StringTokenizer(reader.readLine())
    }
    tokenizer.nextToken()
  }
}
