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>

using namespace std;

int n;
const int N = 110;
int A[N][N];
typedef pair<int, int> P;
vector<P> G[N];

struct Edge {
    int u, v, cost;
    
    bool operator<(const Edge& other) const {
        return cost < other.cost;
    }
};

vector<Edge> edges;
vector<int> parent(N), height(N);

int min_cost = 0;

void prim() {
    priority_queue<P, vector<P>, greater<P>> heap;
    heap.push({0, 1});
    vector<bool> vis(n + 1, false);
    vector<int> dis(n + 1, INT_MAX);

    while (!heap.empty()) {
        int u = heap.top().second, cost = heap.top().first;
        heap.pop();
        if (vis[u]) continue;
        vis[u] = true;
        min_cost += cost;
        for (int i = 0; i < G[u].size(); i++) {
            int v = G[u][i].second, len = G[u][i].first;
            if (!vis[v] && len < dis[v]) {
                dis[v] = len;
                heap.push({len, v});
            }
        }
    }    
}

void init() {
  for (int i = 1; i <= n; i++) {
    parent[i] = i;
    height[i] = 1;
  }
}

int find(int x) {
  if (parent[x] == x) return x;
  return parent[x] = find(parent[x]);
}

void merge(int x, int y) {
    x = find(x), y = find(y);
    if (x == y) return;
    if (height[x] < height[y]) {
        parent[x] = y;
    } else {
        parent[y] = x;
        if (height[x] == height[y]) height[x]++;
    }
}

void kruskal() {
    sort(edges.begin(), edges.end());
    init();
    for (auto &edge : edges) {
        int u = edge.u, v = edge.v, cost = edge.cost;
        
        if (find(u) != find(v)) {
            merge(u, v);
            min_cost += cost;
        }
    }
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> A[i][j];
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = i + 1; j <= n; j++) {
            if (A[i][j] != -1)
            edges.push_back({i, j, A[i][j]});
        }
    }

    kruskal();
    // prim();
    cout << min_cost << endl;

    return 0;
}

C++: #include<iostream>
#include<cstring>
using namespace std;
#define maxn 110
#define INFTY (1<<30)
#define WHITE 0
#define GRAY 1
#define BLACK 2
int n,m[maxn][maxn];

int prim() //返回最小生成树各边权值总和
{
    int u,minv;
    int d[maxn],p[maxn],color[maxn];

    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) //如果u和v之间存在边
            {
                if(m[u][v]<d[v]) //注意这里是d[v]不是d[u]
                {
                    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++)
        {
            cin>>m[i][j];
            if(m[i][j]==-1) m[i][j]=INFTY;
        }
    }
    cout<<prim()<<endl;
    return 0;
}

C++: #include<vector>
#include<iostream>
#include<queue>
#include<functional>
using namespace std;

constexpr int INF = 1e9;

struct edge {
	int from, to, cost;
	bool operator>(const edge& other)const {
		return cost > other.cost;
	}
};

using edges = vector<edge>;
using graph = vector<edges>;

void add_edge(graph& g, int from, int to, int cost) {
	g[from].push_back(edge{ from,to,cost });
	g[to].push_back(edge{ to,from,cost });
}


edges prim(const graph& g) {
	const int N = g.size();
	vector<bool>used(N);
	used[0] = true;
	priority_queue<edge, vector<edge>, greater<edge>>p_que;
	for (auto e : g[0]) {
		p_que.push(e);
	}
	edges mst;
	while (!p_que.empty()) {
		auto e = p_que.top();
		p_que.pop();
		if (used[e.to]) {
			continue;
		}
		mst.push_back(e);
		used[e.to] = true;
		for (auto new_e : g[e.to]) {
			p_que.push(new_e);
		}

	}
	return mst;
}

int main() {
	int V, E;
	cin >> V >> E;
	graph g(V);
	for (int i = 0; i < E; i++) {
		int from, to, cost;
		cin >> from >> to >> cost;
		add_edge(g, from, to, cost);
	}
	const auto mst = prim(g);
	int ans = 0;
	for (auto& e : mst) {
		ans += e.cost;
	}
	cout << ans << endl;
	return 0;
}

C++: #include <iostream>
#include <utility>
#include <algorithm>
#define MAX 150
#define rep(i,p) for(int i=0;i<p;i++)
#define rep1(i,p) for(int i=1;i<=p;i++)
using namespace std;
int n,temp,a,b,size,ra,rb;
int A[MAX];
int B[MAX];
int W[MAX];
int V[MAX];
pair<int,int> edge[MAX];
int root (int child) {
  int a = child;
  while (V[a] != a) {
    a = V[a];
  }
  return a;
}
int main(){
  size = 0;
  cin >> n;
  rep(i,n) {
    rep(j,n) {
      cin >> W[size];
      if (i <= j && W[size] != -1) { 
        // cout <<  A[size] << " " << B[size] << " " << j << endl;        
        A[size] = i;
        B[size] = j;        
        edge[size].first = W[size];
        edge[size].second = size;
        size++;
      }
    }
  }
  // cout << endl;
  // rep(i,size) {
  //   cout << A[edge[i].second] << " " << B[edge[i].second]
  //        << " " << edge[i].first << endl;;
  // }
  // sort by lenght of edge
  sort(edge, edge+size+1);
  
  int sum = 0;
  rep(i,n) V[i] = i;
  rep(i,size) {
    if (edge[i].first == -1) continue;
    temp = edge[i].second;
    // a , b : vertex
    a = A[temp];
    b = B[temp];
    ra = root(a);
    rb = root(b);
    if (ra != rb) {
      // ?????????????????????
      sum += edge[i].first;
      V[ra] = rb;
      // if (ra != a && rb == b){     
      //   V[b] = ra;
      // } else if (ra == a && rb != b) {
      //   V[a] = rb;
      // } else if (ra == a && rb == b) {        
      //   V[a] = b;
      // } else if (ra != a && rb != b) {                
      //   V[ra] = rb;
      // }
    }
  }
  cout << sum << endl;
  return 0;
}