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<iostream>
#include<vector>
#include<queue>

using namespace std;
using Graph=vector<vector<int>>;



struct Edge {
	int node1, node2, weight;

	Edge() {}

	Edge(int n1, int n2, int w) :node1(n1), node2(n2), weight(w) {}

	void numset(int n1, int n2, int w) {
		node1 = n1; node2 = n2; weight = w;
	}

};

void eswap(Edge& e1, Edge& e2) {
	Edge temp = e1;   e1 = e2;  e2 = temp;
}

void bubbleSort(vector<Edge>& edge) {
	bool flag;
	for (int i = 0; i < edge.size(); i++) {
		flag = true;
		for (int j = 0; j < edge.size()-i-1; j++) {
			if (edge.at(j).weight > edge.at(j + 1).weight) {
				eswap(edge.at(j), edge.at(j + 1));
				flag = false;
			}
		}
		if (flag) break;
	}
}

int kruskal(Graph &spntree,vector<Edge>& edgeset) {
	int size = spntree.size();
	queue<int> unconnected;
	vector<bool> connected(size,false);
	
	if (edgeset.size() == 0) return 0;
	bubbleSort(edgeset);

	connected.at(edgeset.at(0).node1)=true;
	connected.at(edgeset.at(0).node2) = true;
	spntree.at(edgeset.at(0).node1).push_back(edgeset.at(0).node2);
	spntree.at(edgeset.at(0).node2).push_back(edgeset.at(0).node1);
	int index=0,count = 2,cost=edgeset.at(0).weight;
	Edge ed;
	edgeset.erase(edgeset.begin());

	while (count<size) {
		ed = edgeset.at(index);
		if (connected.at(ed.node1) == true && connected.at(ed.node2) == false) {
			connected.at(ed.node2) = true;
			spntree.at(ed.node1).push_back(ed.node2);
			spntree.at(ed.node2).push_back(ed.node1);
			edgeset.erase(edgeset.begin() + index);
			count++; cost += ed.weight;
			index = 0; continue;
		}
		else if (connected.at(ed.node2) == true && connected.at(ed.node1) == false) {
			connected.at(ed.node1) = true;
			spntree.at(ed.node1).push_back(ed.node2);
			spntree.at(ed.node2).push_back(ed.node1);
			edgeset.erase(edgeset.begin() + index);
			count++; cost += ed.weight;
			index = 0; continue;
		}
		index++;
	}
	return cost;


}

int main(void) {

	int size, temp,cost,edgenum=0;
	Edge etemp;
	vector<Edge> edgeset;
	cin >> size;
	Graph spntree(size);
	for (int i = 0; i < size; i++) {
		for (int j = 0; j < size; j++) {
			cin >> temp;
			if (i < j) {
				if (temp != -1) {
					edgenum++;
					etemp.numset(i, j, temp);
					edgeset.push_back(etemp);
				}
			}
		}
	}

	cost=kruskal(spntree, edgeset);
	cout << cost << endl;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
using namespace std;
const int maxx = 1010;
const int INF = 1 << 30;
int n, m[maxx][maxx];
int u, num, v;
int flag[maxx];

int prim() {
	// d??°????????????V-T??? p??°????????°???MST???
	int d[maxx], p[maxx];
	for (int i = 0; i < n; i++) {
		d[i] = INF;
		p[i] = -1;
		flag[i] = 0;
	}

	d[0] = 0;
	while (1) {
		int minv = INF;
		u = -1;
		// ???????????????????°???????
		for (int i = 0; i < n; i++) {
			if (minv > d[i] && flag[i] == 0) {
				u = i;
				minv = d[i];
			}
		}

		if (u == -1)
			break;

		// ?????°???????????\MST
		flag[u] = 1;

		// ??´??°?????????????????????????????????????????´???????°???????????????????????????????
		for (int v = 0; v < n; v++) {
			if (flag[v] == 0 && m[u][v] != INF && d[v] > m[u][v]) {
				d[v] = m[u][v];
				p[v] = u;
			}
		}
	}


	int ans = 0;
	for (int i = 0; i < n; i++)
		if (p[i] != -1)
			ans += m[i][p[i]];

	return ans;
}
int main()
{
	memset(flag, 0, sizeof(flag));
	scanf("%d", &n);
	
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			scanf("%d", &m[i][j]);
			if (m[i][j] == -1)
				m[i][j] = INF;
		}
	}
	printf("%d\n", prim());
	return 0;
}

C++: #include <iostream>
#include <array>
#include <vector>
#include <algorithm>

int n;
static const int nmax = 2005;
static const int INFTY = (1 << 21);
static const int WHITE = 0; // Not visited
static const int GRAY = 1;  // Visited
static const int BLACK = 2; // In MST
std::array<std::array<int, nmax>, nmax> adjmat;
std::array<int, nmax> d, p;
std::array<int, nmax> color;

void prim()
{
  color.fill(WHITE);
  d.fill(INFTY);
  p.fill(-1);

  int v = 0;
  d[v] = 0;
  color[v] = GRAY;

  while (true)
    {
      auto mincost = INFTY;

      // Find minimum cost vertex from current MST
      for (int i=0; i<n; ++i)
        {
          if (color[i] != BLACK && d[i] < mincost)
            {
              mincost = d[i];
              v = i;
            }
        }

      // Break if no update
      if (mincost == INFTY) break;

      // Include v in MST
      color[v] = BLACK;

      // Update minimum cost outside current MST
      for (int i=0; i<n; ++i)
        {
          if (color[i] != BLACK && adjmat[v][i] != -1 && adjmat[v][i] < d[i])
            {
              d[i] = adjmat[v][i];
              color[i] = GRAY;
              p[i] = v;
            }
        }
    }
}

int main ()
{
  std::cin >> n;
  for (int i=0; i<n; ++i)
    {
      for (int j=0; j<n; ++j)
        std::cin >> adjmat[i][j];
    }

  prim();

  int ans = 0;
  //std::for_each(d.begin(), d.begin()+n, [&](auto x){ans+=x;});
  for (int i=0; i<n; ++i)
    {
      if (p[i] != -1)
        ans += adjmat[i][p[i]];
    }
  std::cout << ans << std::endl;
}