Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)

	g := make([][]int, n)
	ans := make([][]int, n)

	inf := 1 << 30
	for i := 0; i < n; i++ {
		g[i] = make([]int, n)
		ans[i] = make([]int, n)
		for ii := 0; ii < n; ii++ {
			g[i][ii] = getNextInt(scanner)
			if g[i][ii] == -1 {
				g[i][ii] = inf
			}
			ans[i][ii] = -1
		}
	}

	im := make(IntMap, 1)

	im.increment(1)

	for len(im) < n {
		min := inf
		u := -1
		v := -1
		for i := range im {
			for ii := 0; ii < n; ii++ {
				if im.get(ii) != 0 {
					continue
				}
				if min > g[i][ii] {
					min = g[i][ii]
					u = i
					v = ii
				}
			}
		}
		im.increment(v)
		ans[u][v] = min
		ans[v][u] = min
	}

	sum := 0
	for i := 0; i < n-1; i++ {
		for ii := i + 1; ii < n; ii++ {
			if ans[i][ii] != -1 {
				sum += ans[i][ii]
			}
		}
	}

	fmt.Fprintln(writer, sum)
	writer.Flush()
}

// IntMap ...
type IntMap map[int]int

func (m IntMap) get(key int) int {
	if _, ok := m[key]; ok {
		return m[key]
	}
	return 0
}
func (m IntMap) increment(key int) {
	if _, ok := m[key]; ok == false {
		m[key] = 0
	}
	m[key]++
}

C++: #include<bits/stdc++.h>
#define ll long long
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(i, n) for(int i=0; i<=n; i++)
#define repr(i, n) for(int i=n; i>0; i--)
#define REPR(i, n) for(int i=n; i>=0; i--)
const ll INF = 1LL<<60;
const int INT_INF = 1e9;
const ll MOD = 1e9+7;
template<class T> void puts(T x) { std::cout << x << std::endl; }
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; }
using namespace std;

struct UnionFind {
    vector<int> parent;

    // Constructor
    UnionFind(int n) {
        parent.resize(n);
        rep(i, n) parent[i] = i;
    }

    // データxが属する木の代表元を返す
    int root(int x) {
        if (parent[x] == x) {
            return x;
        } else {
            parent[x] = root(parent[x]);
            return parent[x];
        }
    }

    // データxとyを同じ木にくっつける
    void connect(int x, int y) {
        int rx = root(x);
        int ry = root(y);

        if (rx == ry) return;
        parent[rx] = ry;
    }

    // データxとyが同じ木に属するか判定する
    bool check(int x, int y) {
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};

struct Edge {
    int u;
    int v;
    int weight;

    Edge(int _u, int _v, int w) {
        u = _u;
        v = _v;
        weight = w;
    }

    bool operator >(const Edge other) const {
        return weight > other.weight;
    }

    bool operator ==(const Edge other) const {
        return weight == other.weight;
    }

    bool operator <(const Edge other) const {
        return weight < other.weight;
    }
};

vector<Edge> adj;

int main() {
    int n; cin >> n;
    for (int u = 0; u < n; u++) {
        for (int v = 0; v < n; v++) {
            int w; cin >> w;
            if (v > u && w != -1) adj.emplace_back(Edge(u, v, w));
        }
    }
    sort(adj.begin(), adj.end());

    // for (auto &edge: adj) {
    //     printf("(%d, %d, %d)\n", edge.u, edge.v, edge.weight);
    // }
    // cout << "-----" << endl;

    UnionFind tree(n);
    vector<Edge> s;
    for (auto &edge: adj) {
        // printf("check (u, v, w) = (%d, %d, %d)->", edge.u, edge.v, edge.weight);
        if (!tree.check(edge.u, edge.v)) {
            // printf("connect\n");
            s.emplace_back(edge);
            tree.connect(edge.u, edge.v);
        } else {
            // printf("don't connect\n");
        }
    }

    int ans = 0;
    for (auto &edge: s) {
        // printf("(%d, %d, %d)\n", edge.u, edge.v, edge.weight);
        ans += edge.weight;
    }
    cout << ans << endl;
}

C++: #include <iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<21);

int main()
{
  int n;
  int M[MAX][MAX];

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

  int d[MAX], p[MAX];
  bool visited[MAX];
  for (int i = 0; i < n; ++i)
  {
    d[i] = INFTY;
    p[i] = -1;
    visited[i] = false;
  }
  d[0] = 0;

  while (true)
  {
    int minv = INFTY;
    int u = -1;
    for (int v = 0; v < n; ++v)
    {
      if (!visited[v] && minv > d[v])
      {
        minv = d[v];
        u = v;
      }
    }
    if (u == -1) break;

    visited[u] = true;
    for (int v = 0; v < n; ++v)
    {
      if (!visited[v] && M[u][v] != INFTY)
      {
        if (d[v] > M[u][v])
        {
          d[v] = M[u][v];
          p[v] = u;
        }
      }
    }
  }

  int sum = 0;
  for (int i = 0; i < n; ++i)
  {
    if (p[i] == -1) continue;
    sum += M[i][p[i]];
  }

  cout << sum << endl;

  return 0;
}

C++: #include<iostream>
using namespace std;
#define MAX 100
#define INFTY 1<<21
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n, M[MAX][MAX];

int prim()
{
    int u,minv;
    int d[MAX],p[MAX],color[MAX];

    for(int i=0;i<n;i++)
    {
        d[i]=INFTY;
        p[i]=-1;
        color[i]=WHITE;
    }

    d[0]=0;
    while(1)
    {
        minv=INFTY;
        u=-1;
        for(int i=0;i<n;i++)
        {
            if(minv>d[i] && color[i]!=BLACK)
            {
                u=i;
                minv=d[i];
            }
        }
        if(u==-1)break;

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

int main()
{
    cin >> n;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            int e;
            cin >>e;
            if(e==-1)
            {
                M[i][j]=INFTY;
            }
            else
            {
                M[i][j]=e;
            }
        }
    }

    cout << prim() << endl;
    return 0;
}