Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type ElementKey int
type Node struct {
	key    ElementKey
	parent *Node
	rank   int
}

func newNode(key ElementKey) *Node {
	node := Node{}
	node.key = key
	node.parent = &node
	node.rank = 0

	return &node
}

func union(x, y *Node) {
	xSet := findSet(x)
	ySet := findSet(y)
	link(xSet, ySet)
}

func link(x, y *Node) {
	if x.rank > y.rank {
		y.parent = x
	} else {
		x.parent = y

		if x.rank == y.rank {
			y.rank++
		}
	}
}

func findSet(x *Node) *Node {
	if x != x.parent {
		x.parent = findSet(x.parent)
	}
	return x.parent
}

func newDisjointSets(nSets int) *DisjointSets {
	sets := DisjointSets{}
	sets.keyToNode = map[ElementKey]*Node{}
	for i := 0; i < nSets; i++ {
		key := ElementKey(i)
		sets.keyToNode[key] = newNode(key)
	}
	return &sets
}

type DisjointSets struct {
	keyToNode map[ElementKey]*Node
}

func (sets *DisjointSets) union(x, y ElementKey) {
	xNode := sets.keyToNode[x]
	yNode := sets.keyToNode[y]
	union(xNode, yNode)
}

func (sets DisjointSets) findSet(x ElementKey) *Node {
	xNode := sets.keyToNode[x]
	return findSet(xNode)
}

type Edge struct {
	i, j   int
	weight int
}

func main() {
	scanner.Split(bufio.ScanWords)

	n := nextInt()
	sets := newDisjointSets(n)
	edges := []Edge{}
	a := make([][]int, n)
	for i := 0; i < n; i++ {
		a[i] = make([]int, n)
		for j := 0; j < n; j++ {
			a[i][j] = nextInt()
			if a[i][j] >= 0 {
				e := Edge{
					i:      i,
					j:      j,
					weight: a[i][j],
				}
				edges = append(edges, e)
			}
		}
	}
	sort.Slice(edges, func(i, j int) bool { return edges[i].weight < edges[j].weight })
	totalWeight := 0
	for _, edge := range edges {
		i := ElementKey(edge.i)
		j := ElementKey(edge.j)
		if sets.findSet(i) != sets.findSet(j) {
			totalWeight += edge.weight
			sets.union(i, j)
		}
	}
	fmt.Println(totalWeight)
}

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;
}