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;
using ll = long long;
const ll INF = 1e16;
const ll mod = 1000000007;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}
ll factorial(ll n) {
  if (n == 1) {
    return 1;
  }
  ll ans = factorial(n-1);
  return ans*n;
}
ll gcd(ll a, ll b) {
  if (a < b) {
    ll tmp = a;
    a = b;
    b = tmp;
  }
  ll r = a%b;
  while(r != 0) {
    a = b;
    b = r;
    r = a%b;
  }
  return b;
}
ll lcm(ll a, ll b) {
  return (a/gcd(a, b))*b;
}
bool prime(ll n) {
  if (n == 1) {
    return false;
  } else {
    for (ll i = 2; i <= (ll)pow(n, 0.5); i++) {
      if (n%i == 0) return false;
    }
  }
  return true;
}
ll RepeatSquaring(ll n, ll p, ll m) {
  if (p == 0) return 1;
  if (p%2 == 0) {
    ll t = RepeatSquaring(n, p/2, m);
    return (t*t)%m;
  }
  return (n*RepeatSquaring(n, p-1, m))%m;
}

vector <ll> color(100, 0);
vector <vector <ll>> m(100, vector <ll> (100, INF));
vector <ll> d(100, INF);
vector <ll> p(100, -1);

ll prim(ll n) {
  d.at(0) = 0;
  while (true) {
    ll mincost = INF;
    ll u;
    rep(i, n) {
      if (color.at(i) != 2 && d.at(i) < mincost) {
        mincost = d.at(i);
        u = i;
      }
    }
    if (mincost == INF) {
      break;
    }
    color.at(u) = 2;
    rep(i, n) {
      if (color.at(i) != 2 && m.at(u).at(i) != INF) {
        if (m.at(u).at(i) < d.at(i)) {
          d.at(i) = m.at(u).at(i);
          p.at(i) = u;
          color.at(i) = 1;
        }
      }
    }
  }
  ll sum = 0;
  rep(i, n) {
    if (p.at(i) != -1) sum += m.at(p.at(i)).at(i);
  }
  return sum;
}

int main() {
  ll n;
  cin >> n;
  rep(i, n) {
    rep(j, n) {
      ll a;
      cin >> a;
      if (a != -1) {
        m.at(i).at(j) = a;
      }
    }
  }
  cout << prim(n) << endl;
}

C++: n = int( raw_input() )
A = [0]
p = range(1, n)
a = [ [ 0 for i in xrange(n) ] for j in xrange(n) ]
for i in xrange(n):
    a[i] = map( int,raw_input().split() )
l = 0
while p != []:
    a_min = 2001
    for i in A:
        for j in p:
            if 0 <= a[i][j] < a_min:
                a_min = a[i][j]
                add_A = j
    A.append( add_A )
    NY.remove( add_A )
    l += a_min
print l

C++: #include<iomanip>
#include<limits>
#include<thread>
#include<utility>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pi;
typedef pair<double,double> pd;
typedef pair<double,ll> pdl;
#define F first
#define S second
#define MK make_pair
const ll E=1e18+7;
const ll MOD=1000000007;

vector<ll> uft;

ll search(ll where){
    if(uft[where]==where){return where;}
    else{return uft[where]=search(uft[where]);}
}

void add(ll a,ll b){
    uft[search(a)]=search(b);
}


int main(){
    ll n;
    cin>>n;
    priority_queue<vector<ll>,vector<vector<ll>>,greater<vector<ll>>> E;
    for(int i=0;i<n;i++){
        for(int t=0;t<n;t++){
            ll a;
            cin>>a;
            if(a!=-1){
                E.push({a,i,t});
            }
        }
    }
    uft.resize(n);
    for(int i=0;i<n;i++){
        uft[i]=i;
    }
    ll cost=0;
    while(!E.empty()){
        vector<ll> e=E.top();
        E.pop();
        if(search(e[1])!=search(e[2])){
            cost+=e[0];
            add(e[1],e[2]);
        }
    }
    cout<<cost<<endl;
    
    
    return 0;
}

C++: #include <iostream>
using namespace std;
static const int INFTY = (1 << 21);
int n,M[100][100];

int prim(){
  int u,v,d[100],p[100],color[100],mincost;
  for (int i = 0; i < n; i++) {
    color[i] = 0;
    d[i] = INFTY;
    p[i] = -1;
  }
  d[0] = 0;

  while (1) {
    mincost = INFTY;
    u = -1;
    for (int i = 0; i < n; i++) {
      if (color[i] != 2 && d[i] < mincost) {
        u = i;
        mincost = d[i];
      }
    }
    if (u == -1) break;
    color[u] = 2;
    for (v = 0; v < n; v++) {
      if (color[v] != 2 && M[u][v] != INFTY) {
        if (M[u][v] < d[v]) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = 1;
        }
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != -1) sum += M[i][p[i]];
  }
  return sum;
}

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int e; cin >> e;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }
  cout << prim() << endl;
  return 0;
}