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<iostream>
#include<vector>
#include<map>
#include<limits>
using namespace std;

const int MAX = numeric_limits<int>::max();

class Node{
public:
  int _id;
  bool _isFound;
  vector<int> _adj_index; // 隣接点のid
  vector<int> _adj_vertices;  // 隣接点との距離(重み)

  void init(int id, int n){
    _id = id;
    _isFound = false;
    for(auto i = 0; i < n; i++){
      _adj_vertices.push_back(MAX);
    }
  }
};

void inputGraph(vector<Node>& graph){
  for(auto i = 0; i < graph.size(); i++){
    graph[i].init(i, graph.size());
    for(auto j = 0; j < graph.size(); j++){
      int tmp;
      cin >> tmp;
      if(tmp != -1){
        graph[i]._adj_index.push_back(j);
        graph[i]._adj_vertices[j] = tmp;
      }
    }
  }
}

int mst(vector<Node>& graph, int search){
  auto sum = 0;
  vector<int> searched; // 木に追加済みのNodeのid
  searched.push_back(search);
  graph[searched[0]]._isFound = true;
  int minNodeIndex;

  while(searched.size() < graph.size()){
    auto compare = MAX;

    for(auto i = 0; i < searched.size(); i++){
      for(auto j = 0; j < graph[searched[i]]._adj_index.size(); j++){
//  cout << "call in for " << j << endl;
//  cout << compare << endl;
        Node searching = graph[searched[i]];
        if(!graph[searching._adj_index[j]]._isFound &&
           searching._adj_vertices[searching._adj_index[j]] < compare){
          // 木の隣接点で最短
          compare = searching._adj_vertices[searching._adj_index[j]];
          minNodeIndex = searching._adj_index[j];
 //         cout << "added" << endl;
        }
      }
    }
    if(compare != MAX){
      sum += compare;
      graph[minNodeIndex]._isFound = true;
      searched.push_back(minNodeIndex);
//      cout << endl << "minNodeIndex:" << minNodeIndex << endl;
    }

  }
  return sum;
}

int main(){
  int n;
  cin >> n;
  vector<Node> graph(n);

  inputGraph(graph);
  cout << mst(graph, 0) << endl;

  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int main() {
    int n; cin >> n;

    int adj[n][n];
    int inf = 2001;

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            int k; cin >> k;
            adj[i][j] = k == -1 ? inf : k;
        }
    }

    int sd[n] = {1};
    int ans = 0;

    while(true) {
        int a = -1, c = inf;

        for(int i = 0; i < n; i++) {
            if(sd[i] == 0) { 
                if(a == -1) {
                    a = i;
                }
                continue;
            }
            for(int j = 0; j < n; j++) {
                if(adj[i][j] < c && sd[j] == 0) {
                    c = adj[i][j];
                    a = j;
                }
            }
        }

        if(a == -1) break;

        ans += c;
        sd[a] = 1;
    }

    cout << ans << endl;

}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include<map>
#include<set>
#include<queue>

typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000000;
const int INFTY = (1 << 21); // 2097152
// const ll INFTY = (1 << 60);

class Edge{
public:
	int from, to, cost;
	Edge(int from = 0, int to = 0, int cost = 0) :
			from(from), to(to), cost(cost){}

	bool operator < (const Edge &e) const{
		return cost < e.cost;
	}
	bool operator >(const Edge &e) const{
		return cost > e.cost;
	}

};

class Node{
public:
	vector<Edge> edges;
	int dist;

	Node(){
		dist = INFTY;
	}
};

class Graph{
public:
	vector<Node> nodes;
	Graph(int n) {
		rep(i, n) { Node n;  nodes.push_back(n); }
	}
	Node& operator[](int i){
		return nodes[i];
	}
};

class Data{

public:
	int dist;
	Edge *edge;
	Data(int dist, Edge *edge) {
		this->dist = dist;
		this->edge = edge;
	}

	bool operator < (const Data &d) const{
		return dist < d.dist;
	}
	bool operator >(const Data &d) const{
		return dist > d.dist;
	}
};

Graph G(0);
int N;

int prim(){

    priority_queue<Edge, vector<Edge>, greater<Edge>> pq;
    Edge e0 = Edge(-1, 0, 0);
    pq.push(e0);

    while(!pq.empty()){
        Edge e = pq.top(); pq.pop();
        Node &n = G[e.to];
        if(n.dist != INFTY) continue;
        n.dist = e.cost;
        // cerr << n.dist << endl;

        for(auto &ee: n.edges){
            pq.push(ee);
        }
    }

    int ret = 0;
    rep(i, N) ret += G[i].dist;
    return ret;
}

int main() {
	cin >> N;
	G = Graph(N);

    rep(i, N){
        rep(j, N) {
            int cost;
            cin >> cost;
            if(cost != -1){
                int from = i; int to=j;
                G[from].edges.push_back(Edge(from, to, cost));
            }
        }
    }

	cout << prim() << endl;
}