import sys



def debug(x, table):

    for name, val in table.items():

        if x is val:

            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)

            return None



def solve():

    inf = float('inf')

    n = int(input())

    a = []



    for i in range(n):

        line = [int(i) for i in input().split()]

        line = [i if i != -1 else inf for i in line]

        a.append(line)



    

    checked = [False] * n

    d = [inf] * n

    d[0] = 0

    checked[0] = True

    ans = 0

    nxt = 0



    for i in range(n - 1):

        for j in range(n):

            if a[nxt][j] < d[j]:

                d[j] = a[nxt][j]



        min_w = inf

        min_n = 0



        for j in range(n):

            if not checked[j] and d[j] < min_w:

                min_n = j

                min_w = d[j]



        checked[min_n] = True

        nxt = min_n

        ans += min_w



    # debug(d, locals())

    print(ans)







if __name__ == '__main__':

    solve()

import sys



inf = float('inf')



def debug(x, table):

    for name, val in table.items():

        if x is val:

            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)

            return None



def prim(n, As):

    checked = [False] * n

    d = [inf] * n

    checked[0] = True

    d[0] = 0

    nxt = 0

    tot_wgt = 0



    for i in range(n - 1):

        for j in range(n):

            if As[nxt][j] < d[j]:

                d[j] = As[nxt][j]



        min_w = inf

        min_n = 0

        for j in range(n):

            if not checked[j] and min_w > d[j]:

                min_w = d[j]

                min_n = j



        tot_wgt += min_w

        nxt = min_n

        checked[nxt] = True



    return tot_wgt



def solve():

    n = int(input())

    As = []



    for i in range(n):

        line = [int(j) for j in input().split()]

        As.append([i if i != -1 else inf for i in line])



    ans = prim(n, As)



    print(ans)





if __name__ == '__main__':

    solve()

import sys


def debug(x, table):
    for name, val in table.items():
        if x is val:
            print("DEBUG:{} -> {}".format(name, val), file=sys.stderr)
            return None


def solve():
    inf = float("inf")
    n = int(input())
    a = []
    for i in range(n):
        line = [int(i) for i in input().split()]
        line = [i if i != -1 else inf for i in line]
        a.append(line)
    checked = [False] * n
    d = [inf] * n
    d[0] = 0
    checked[0] = True
    ans = 0
    nxt = 0
    for i in range(n - 1):
        for j in range(n):
            if a[nxt][j] < d[j]:
                d[j] = a[nxt][j]
        min_w = inf
        min_n = 0
        for j in range(n):
            if not checked[j] and d[j] < min_w:
                min_n = j
                min_w = d[j]
        checked[min_n] = True
        nxt = min_n
        ans += min_w
    # debug(d, locals())
    print(ans)


if __name__ == "__main__":
    solve()

import sys

inf = float("inf")


def debug(x, table):
    for name, val in table.items():
        if x is val:
            print("DEBUG:{} -> {}".format(name, val), file=sys.stderr)
            return None


def prim(n, As):
    checked = [False] * n
    d = [inf] * n
    checked[0] = True
    d[0] = 0
    nxt = 0
    tot_wgt = 0
    for i in range(n - 1):
        for j in range(n):
            if As[nxt][j] < d[j]:
                d[j] = As[nxt][j]
        min_w = inf
        min_n = 0
        for j in range(n):
            if not checked[j] and min_w > d[j]:
                min_w = d[j]
                min_n = j
        tot_wgt += min_w
        nxt = min_n
        checked[nxt] = True
    return tot_wgt


def solve():
    n = int(input())
    As = []
    for i in range(n):
        line = [int(j) for j in input().split()]
        As.append([i if i != -1 else inf for i in line])
    ans = prim(n, As)
    print(ans)


if __name__ == "__main__":
    solve()

+ +inf = float("inf") -def solve(): -    inf = float("inf") -    n = int(input()) -    a = [] -    for i in range(n): -        line = [int(i) for i in input().split()] -        line = [i if i != -1 else inf for i in line] -        a.append(line) +def prim(n, As): +    checked[0] = True -    checked[0] = True -    ans = 0 +    tot_wgt = 0 -            if a[nxt][j] < d[j]: -                d[j] = a[nxt][j] +            if As[nxt][j] < d[j]: +                d[j] = As[nxt][j] -            if not checked[j] and d[j] < min_w: +            if not checked[j] and min_w > d[j]: +                min_w = d[j] -                min_w = d[j] -        checked[min_n] = True +        tot_wgt += min_w -        ans += min_w -    # debug(d, locals()) +        checked[nxt] = True +    return tot_wgt + + +def solve(): +    n = int(input()) +    As = [] +    for i in range(n): +        line = [int(j) for j in input().split()] +        As.append([i if i != -1 else inf for i in line]) +    ans = prim(n, As)