local n = io.read("*n")
local edge = {}
for i = 1, n do
  for j = 1, n do
    edge[(i - 1) * n + j] = io.read("*n")
  end
end
local totallen = 0
local err = false
for dst = 2, n do
  for src = 1, dst - 1 do
    local sdlen = edge[(src - 1) * n + dst]
    local shortest = true
    for mid = 1, n do
      if src ~= mid and dst ~= mid then
        local smlen, mdlen = edge[(src - 1) * n + mid], edge[(mid - 1) * n + dst]
        if smlen + mdlen < sdlen then
          err = true
        elseif smlen + mdlen == sdlen then
          shortest = false
        end
      end
    end
    if shortest then totallen = totallen + sdlen end
  end
end
print(err and -1 or totallen)
