def solv(G):

  new_V = [0]

  new_E = []



  while len(new_V) < len(G):

    u, e = select_min(G, new_V)

    new_V.append(u)

    new_E.append(e)



  return sum(new_E)



def select_min(G, V):

  min_u = -1

  min_e = float('inf')

  for v in V:

    for u, e in enumerate(G[v]):

      if (e < 0) or (u in V): continue

      if e < min_e:

        min_u = u

        min_e = e

  return min_u, min_e



n = int(eval(input()))

G = [list(map(int, input().split())) for _ in range(n)]

print((solv(G)))
def solv(G):

  n = len(G)

  V = {0:0}

  new_V = []

  cost = 0



  while len(new_V) < n:

    u, e = min(list(V.items()), key=lambda x:x[1])

    V.pop(u)

    new_V.append(u)

    cost += e

    for v, e in enumerate(G[u]):

      if e < 0 or v == u or v in new_V: continue

      V[v] = min(V[v], e) if v in V else e



  return cost



n = int(eval(input()))

G = [list(map(int, input().split())) for _ in range(n)]

print((solv(G)))
def solv(G):
    new_V = [0]
    new_E = []
    while len(new_V) < len(G):
        u, e = select_min(G, new_V)
        new_V.append(u)
        new_E.append(e)
    return sum(new_E)


def select_min(G, V):
    min_u = -1
    min_e = float("inf")
    for v in V:
        for u, e in enumerate(G[v]):
            if (e < 0) or (u in V):
                continue
            if e < min_e:
                min_u = u
                min_e = e
    return min_u, min_e


n = int(eval(input()))
G = [list(map(int, input().split())) for _ in range(n)]
print((solv(G)))

def solv(G):
    n = len(G)
    V = {0: 0}
    new_V = []
    cost = 0
    while len(new_V) < n:
        u, e = min(list(V.items()), key=lambda x: x[1])
        V.pop(u)
        new_V.append(u)
        cost += e
        for v, e in enumerate(G[u]):
            if e < 0 or v == u or v in new_V:
                continue
            V[v] = min(V[v], e) if v in V else e
    return cost


n = int(eval(input()))
G = [list(map(int, input().split())) for _ in range(n)]
print((solv(G)))

-    new_V = [0] -    new_E = [] -    while len(new_V) < len(G): -        u, e = select_min(G, new_V) +    n = len(G) +    V = {0: 0} +    new_V = [] +    cost = 0 +    while len(new_V) < n: +        u, e = min(list(V.items()), key=lambda x: x[1]) +        V.pop(u) -        new_E.append(e) -    return sum(new_E) - - -def select_min(G, V): -    min_u = -1 -    min_e = float("inf") -    for v in V: -        for u, e in enumerate(G[v]): -            if (e < 0) or (u in V): +        cost += e +        for v, e in enumerate(G[u]): +            if e < 0 or v == u or v in new_V: -            if e < min_e: -                min_u = u -                min_e = e -    return min_u, min_e +            V[v] = min(V[v], e) if v in V else e +    return cost