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 <stdio.h>
#include <vector>

using namespace std;

struct Wside{
	int vertex;
	int weight;
	Wside(int a, int b){
		vertex=a; weight=b;
	}
};

vector<Wside>* info;
vector<int> scanned;
int n;
int minimum;

bool isScanned(int v){
	for(int i=0,sz=scanned.size(); i<sz; ++i){
		if(v==scanned[i]) return 1;
	}
	return 0;
}

int scan(){
	int temp_min=100000;
	int temp_v = 0;
	for(int i=0, sz=scanned.size(); i<sz; ++i){
		int s=scanned[i];
		for(int j=0, sz2=info[s].size(); j<sz2; ++j){
			if(isScanned(info[s][j].vertex)){
				continue;
			}
			temp_min = temp_min<info[s][j].weight? temp_min: info[s][j].weight;
			temp_v = temp_min<info[s][j].weight? temp_v: info[s][j].vertex;
		}
	}

	minimum += temp_min;
	scanned.push_back(temp_v);
	return 0;
}

int main()
{
	scanf("%d",&n);
	info = new vector<Wside> [n];

	int temp;
	for(int i=0; i<n; ++i){
		for(int j=0; j<n; ++j){
			scanf("%d",&temp);
			if(temp!=-1){
				info[i].push_back(Wside(j, temp));
			}
		}
	}

	scanned.push_back(0);
	while(scanned.size()!=n){
		scan();
	}
	cout << minimum << endl;

	delete[] info;

	return 0;
}

C++: #include<iostream>
#include<utility>
#include<algorithm>
using namespace std;

int n;
int cost[1000010],src[10010],dst[10010];
int a[110][110];
int P[10010];
int M;
class edges{
public:
    int x;
    int y;
    int costs;
}edges[1000010];
bool comp(class edges a,class edges b)
{
    return a.costs<b.costs;
}
void init(int N) {
    for (int i=1; i<=N; ++i)
        P[i] = i;
}

int root(int a) {
    if (P[a] == a)
        return a;
    return (P[a] = root(P[a]));
}

bool same(int a, int b) {
    return root(a) == root(b);
}

void unite(int a, int b) {
    P[root(a)] = root(b);
}

void inits(){
    for(int r=0;r<n;r++){
        for(int c=r+1;c<n;c++){
            if(a[r][c]>-1){
                cost[M]=a[r][c];
                src[M]=r;
                dst[M]=c;
                M++;
            }
        }
    }
    for(int i=0;i<M;i++){
        edges[i].x=src[i];
        edges[i].y=dst[i];
        edges[i].costs=cost[i];
}
    sort(edges,edges+M,comp);
}
int solve()
{
    int sum=0;
    for(int i=0;i<M;i++){
        if(same(edges[i].x,edges[i].y))
            continue;
        unite(edges[i].x,edges[i].y);
        sum+=edges[i].costs;
    }
    return sum;
}

int main(){
    cin>>n;
    init(n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++)
        cin>>a[i][j];
    }
    inits();
    cout<<solve()<<endl;
    return 0;
}

C++: #include <iostream>

#define INF 10000

using namespace std;

int main(){
  int n; cin >> n;
  int G[n+1][n+1];
  for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++){
      cin >> G[i][j];
    }
  }
  //prim's algorithm
  int d[n+1],visited[n+1];
  int sum=0;
  for(int i=1;i<=n;i++){
    visited[i]=0;
    d[i]=INF;
  }
  d[1]=0;
  while(1){
    int mincost=INF;
    int u;
    for(int i=1;i<=n;i++){
      if(visited[i]==0 && d[i]<mincost){
        mincost=d[i];
        u=i;
      }
    }
    if(mincost==INF) break;
    visited[u]=1;
    sum=sum+mincost;
    //update d
    for(int v=1;v<=n;v++){
      if(visited[v]==0 && G[u][v]!=-1){
        if(G[u][v]<d[v]){
          d[v]=G[u][v];
        }
      }
    }
  }
  cout << sum << endl;
}

C++: #include <iostream>
#include <stdio.h>
#include <cstring>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <utility>
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
using namespace std;

int main(){
	
	int n;
	cin>>n;
	vector<vector<int>> a(n);
	rep(i,n){
		a[i].resize(n);
		rep(j,n){
			cin>>a[i][j];
			if(a[i][j]==-1)a[i][j]=INF;
		}
	}
	vector<int> t(n);
	rep(i,n){
		t[i]=0;
	}
	t[0]=1;
	int ans=0;
	rep(ii,n-1){
		int m=100000;
		int pm;
		rep(i,n){
			rep(j,n){
				if(t[i]==1 && t[j]==0 && m>a[i][j]){
					m=a[i][j];
					pm=j;
				}
			}
		}
		t[pm]=1;
		ans+=m;
	}
	cout<<ans<<endl;
	
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#define MAX 2000200
#define MIN -2000000300
#define INF 1<<30
using namespace std;
int n;
int A[1000][1000];
int d[1000];
bool used[1000];

void Init(int n){
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      A[i][j] = INF;
    }
  }
}

int prime(){
  for(int i=0;i<n;i++){
    d[i] = INF;
    used[i]=false;
  }
  int ans=0;
  d[0] = 0;

  while(true){
    int v = -1;
    for(int i=0;i<n;i++){
      if(!used[i] && (v == -1 || d[v] > d[i]))v = i;
    }
    if(v == -1)break;
    ans+=d[v];
    used[v] = true;   

    for(int i=0;i<n;i++){
      d[i] = min(d[i],A[v][i]);
    }

  }
  return ans;
}

int main(){
  scanf("%d",&n);
  //  Init(n);
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      scanf("%d",&A[i][j]);
      if(A[i][j] == -1)A[i][j] = INF;
    }
  }
 
  printf("%d\n",prime());
 

  return 0;
}