Go: package main

import (
	"sort"
	"bufio"
	"os"
	"fmt"
	"strconv"
)

// Define disjoint-set forests

type vertexNo int

type vertex struct {
	number vertexNo
	rank int
	parent *vertex
}

func makeSet(number vertexNo) vertex {
	return vertex {
		number: number,
		rank: 0,
		parent: nil,
	}
}

func union(x, y *vertex) *vertex {
	return link(findSet(x), findSet(y))
}

func link(x, y *vertex) *vertex {
	if x.rank > y.rank {
		y.parent = x
		return x
	} else {
		x.parent = y
		if x.rank == y.rank {
			y.rank += 1
		}
		return y
	}
}

func findSet(v *vertex) *vertex {
	if v.parent == nil {
		return v
	}
	v.parent = findSet(v.parent)
	return v.parent
}

// Function to calculate minimum spanning tree

type matrix [][]int

type edge struct {
	one vertexNo
	two vertexNo
	weight int
}

const(
	MAX_VERTICES = 100
)

func newEdgesFromMatrix(matrix matrix) []edge {
	edges := make([]edge, 0, MAX_VERTICES * MAX_VERTICES)

	for i := 0; i < len(matrix); i++ {
		row := matrix[i]
		for j := i; j < len(row); j++ {
			if row[j] >= 0 {
				edge := edge{one: vertexNo(i+1), two: vertexNo(j+1), weight: row[j]}
				edges = append(edges, edge)
			}
		}
	}

	return edges
}

func solve(matrix matrix) int {
	nVertices := len(matrix)

	// initialize vertices
	vertices := make([]vertex, nVertices)
	for i := 0; i < nVertices; i++ {
		vertices[i] = makeSet(vertexNo(i+1))
	}

	// prepare edges sorted in the ascending order of weight
	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	unionCount := 0
	weightSum := 0
	for unionCount < nVertices-1 {
		for i := 0; i < len(edges); i++ {
			edge := edges[i]
			v1 := &vertices[edge.one-1]
			v2 := &vertices[edge.two-1]
			rep1 := findSet(v1)
			rep2 := findSet(v2)
			if rep1.number == rep2.number {
				continue
			}
			union(rep1, rep2)
			edges = append(edges[0:i], edges[i+1:]...)
			weightSum += edge.weight
			unionCount++
			break
		}
	}

	return weightSum
}

// main

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	matrix := parseMatrix(n, sc)

	edges := newEdgesFromMatrix(matrix)
	sort.Slice(edges, func(i, j int) bool {
		return edges[i].weight < edges[j].weight
	})

	fmt.Println(solve(matrix))
}

func parseMatrix(n int, sc *bufio.Scanner) [][]int {
	matrix := make([][]int, n)
	for i := 0; i < n; i++ {
		matrix[i] = make([]int, n)
		for j := 0; j < n; j++ {
			x, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			matrix[i][j] = x
		}
	}
	return matrix
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

func nextInt(sc *bufio.Scanner) (int, error) {
	return strconv.Atoi(nextText(sc))
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef int64_t i64;
typedef uint64_t ui64;

class djset{
private:
    int sz;
    int *par;
public:
    djset(int n){
        sz=n;
        par=new int[sz];
        for(int i=0;i<sz;i++) par[i]=1;
    }
    int find(int x){
        return par[x]<=0?-(par[x]=-find(-par[x])):x;
    }
    void unite(int x,int y){
        int px=find(x),py=find(y);
        if(px==py) return;
        if(par[px]<par[py]){
            int tmp=px;
            px=py;
            py=tmp;
        }
        par[px]+=par[py];
        par[py]=-px;
    }
    bool same(int x,int y){
        return (find(x)==find(y));
    }
    int size(int x){
        return par[find(x)];
    }
    int group(void){
        int cnt=0;
        for(int i=0;i<sz;i++){
            if(par[i]>0) cnt++;
        }
        return cnt;
    }
    void refr(void){
        delete[] par;
    }
};

class graph{
private:
    struct vrtx{
        int to;
        int64_t cost;
    };
    struct adj{
        int start;
        int to;
        int64_t cost;
    };
    int nd;
    int edg;
public:
    std::vector<vrtx> *node;
    std::vector<adj> edge;
    graph(int n,int m){
        nd=n;
        edg=m;
        node=new std::vector<vrtx>[nd];
    }
    void add_dir(int s,int t,int64_t c){
        node[s].push_back({t,c});
        edge.push_back({s,t,c});
    }
    void add_undir(int s,int t,int64_t c){
        node[s].push_back({t,c});
        node[t].push_back({s,c});
        edge.push_back({s,t,c});
    }
    void dlt_dir(int s,int t){
        for(int i=0;i<node[s].size();i++){
            if(node[s][i].to==t){
                node[s].erase(node[s].begin()+i);
                break;
            }
        }
    }
    void dlt_undir(int s,int t){
        for(int i=0;i<node[s].size();i++){
            if(node[s][i].to==t){
                node[s].erase(node[s].begin()+i);
                break;
            }
        }
        for(int i=0;i<node[t].size();i++){
            if(node[t][i].to==s){
                node[t].erase(node[t].begin()+i);
                break;
            }
        }
    }
    void refr(void){
        for(int i=0;i<nd;i++) std::vector<vrtx>().swap(node[i]);
        delete[] node;
        std::vector<adj>().swap(edge);
    }
    bool islnk(void);
    void dfs(int r,int64_t **p,bool **vst);  // 木, (*p)[], vst[] を呼び出す前に初期化
    bool ispls(void);
    void bellford(int s,int64_t **d);
    void dijkstra(int s,int64_t **d);  // 負の閉路なし
    void bfs(int s,int64_t **d);  // 辺に重み無し or 木
    void waflo(bool isdir,int64_t ***d);
    int64_t minstw_dir(int r);  // class: djset, class: skewheap が必要
    int64_t minstw_undir(void);  // class: djset が必要
    void edmonds(int r,std::vector<std::pair<int,int>> &p);  // class: djset, class: skewheap が必要
    void kruscal(std::vector<std::pair<int,int>> &p);  // class: djset が必要
    void prim(std::vector<std::pair<int,int>> &p);
    void cumulsum(int s,int64_t **d);  // 木
    void cumulprd(int s,int64_t **d);  // 木
};

void graph::kruscal(std::vector<std::pair<int,int>> &p){
    adj *es=new adj[edg];
    for(int i=0;i<edg;i++) es[i]={edge[i].start,edge[i].to,edge[i].cost};
    std::sort(es,es+edg,[](const adj &e1,const adj &e2){return e1.cost<e2.cost;});
    djset uf(nd);
    for(int i=0;i<edg;i++){
        if(!uf.same(es[i].start,es[i].to)){
            uf.unite(es[i].start,es[i].to);
            p.push_back({es[i].start,es[i].to});
        }
    }
}

int64_t graph::minstw_undir(void){
    adj *es=new adj[edg];
    for(int i=0;i<edg;i++) es[i]={edge[i].start,edge[i].to,edge[i].cost};
    std::sort(es,es+edg,[](const adj &e1,const adj &e2){return e1.cost<e2.cost;});
    djset uf(nd);
    int64_t res=0;
    for(int i=0;i<edg;i++){
        if(!uf.same(es[i].start,es[i].to)){
            uf.unite(es[i].start,es[i].to);
            res+=es[i].cost;
        }
    }
    return res;
}

int main(void){
    int n;
    scanf("%i",&n);
    int m=0;
    i64 **d=new i64*[n];
    for(int i=0;i<n;i++){
        d[i]=new i64[n];
        for(int j=0;j<n;j++){
            scanf("%lli",&d[i][j]);
            if(d[i][j]!=-1) m++;
        }
    }
    graph g(n,m);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(d[i][j]!=-1) g.add_dir(i,j,d[i][j]);
        }
    }
    printf("%lli\n",g.minstw_undir());
    return 0;
}