module S = Set.Make (struct
  type t = int * int
  let compare (_, x) (_, y) = x - y
end)

let prim a n =
  let reached_p = Array.make n false in
  let d = Array.make n max_int in
  d.(0) <- 0;
  let rec doit set =
    if S.is_empty set then ()
    else begin
      let (u, w) = S.min_elt set in
      let set = S.remove (u, w) set in
      reached_p.(u) <- true;
      List.fold_left (fun set (v, c) ->
        if reached_p.(v) || c >= d.(v) then set
        else begin
          d.(v) <- c;
          S.add (v, c) set
        end)
        set a.(u)
      |> doit
    end in
  S.(empty |> add (0, 0)) |> doit;
  Array.fold_left (fun sum c -> if c = max_int then sum else sum + c) 0 d

let () =
  let n = read_int () in
  let a = Array.make n [] in
  for i = 0 to n - 1 do
    for j = 0 to n - 1 do
      let x = Scanf.scanf " %d" (fun i -> i) in
      if x <> (-1) then a.(i) <- (j, x) :: a.(i);
    done
  done;
  prim a n |> Printf.printf "%d\n"