n = int(eval(input()))



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



def mst():

    T = [0]

    gross_weight = 0

    cnt = n - 1

    while cnt:

        ew = 2001

        for i in range(len(T)):

            for tv, tew in enumerate(adj[T[i]]):

                if (tv not in T) and (tew != -1):

                    if tew < ew:

                        v, ew = tv, tew

        T.append(v)

        gross_weight += ew

        cnt -= 1

    print(gross_weight)





mst()
n = int(eval(input()))



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





def prim_mst(vn):

    isVisited = [False] * vn

    d = [0] + [2001] * (vn - 1)

    p = [-1] * vn



    while True:

        mincost = 2001

        for i in range(vn):

            if (not isVisited[i]) and (d[i] < mincost):

                mincost = d[i]

                u = i



        if mincost == 2001:

            break



        isVisited[u] = True



        for v in range(vn):

            if (not isVisited[v]) and (adj[u][v] != -1):

                if adj[u][v] < d[v]:

                    d[v] = adj[u][v]

                    p[v] = u



    print((sum(d)))





prim_mst(n)
n = int(eval(input()))
adj = [list(map(int, input().split())) for i in range(n)]


def mst():
    T = [0]
    gross_weight = 0
    cnt = n - 1
    while cnt:
        ew = 2001
        for i in range(len(T)):
            for tv, tew in enumerate(adj[T[i]]):
                if (tv not in T) and (tew != -1):
                    if tew < ew:
                        v, ew = tv, tew
        T.append(v)
        gross_weight += ew
        cnt -= 1
    print(gross_weight)


mst()

n = int(eval(input()))
adj = [list(map(int, input().split())) for i in range(n)]


def prim_mst(vn):
    isVisited = [False] * vn
    d = [0] + [2001] * (vn - 1)
    p = [-1] * vn
    while True:
        mincost = 2001
        for i in range(vn):
            if (not isVisited[i]) and (d[i] < mincost):
                mincost = d[i]
                u = i
        if mincost == 2001:
            break
        isVisited[u] = True
        for v in range(vn):
            if (not isVisited[v]) and (adj[u][v] != -1):
                if adj[u][v] < d[v]:
                    d[v] = adj[u][v]
                    p[v] = u
    print((sum(d)))


prim_mst(n)

-def mst(): -    T = [0] -    gross_weight = 0 -    cnt = n - 1 -    while cnt: -        ew = 2001 -        for i in range(len(T)): -            for tv, tew in enumerate(adj[T[i]]): -                if (tv not in T) and (tew != -1): -                    if tew < ew: -                        v, ew = tv, tew -        T.append(v) -        gross_weight += ew -        cnt -= 1 -    print(gross_weight) +def prim_mst(vn): +    isVisited = [False] * vn +    d = [0] + [2001] * (vn - 1) +    p = [-1] * vn +    while True: +        mincost = 2001 +        for i in range(vn): +            if (not isVisited[i]) and (d[i] < mincost): +                mincost = d[i] +                u = i +        if mincost == 2001: +            break +        isVisited[u] = True +        for v in range(vn): +            if (not isVisited[v]) and (adj[u][v] != -1): +                if adj[u][v] < d[v]: +                    d[v] = adj[u][v] +                    p[v] = u +    print((sum(d))) -mst() +prim_mst(n)