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;
#define N 100
int G[N][N];
int n;

int prim()
{
    int visited[n];
    int d[n];
    int p[n];

    for (int i; i < n; i++)
    {
        visited[i] = 0;
        d[i] = 114514;
    }

    d[0] = 0;
    p[0] = -1;

    while (true)
    {
        int mincost = 114514;
        int u;
        for (int i = 0; i < n; i++)
        {
            if (!visited[i] && d[i] < mincost)
            {
                mincost = d[i];
                u = i;
            }
        }

        if (mincost == 114514)
            break;

        visited[u] = 1;

        for (int i = 0; i < n; i++)
        {
            if (!visited[i] && G[u][i] != -1)
            {
                if (G[u][i] < d[i])
                {
                    d[i] = G[u][i];
                    p[i] = u;
                }
            }
        }
    }

    int sum = 0;
    for (int i = 1; i < n; i++)
        sum += d[i];
    return sum;
}

int main()
{
    cin >> n;
    int a;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            cin >> a;
            if (a == -1)
                G[i][j] = 114514;
            else
                G[i][j] = a;
        }
    }
    cout << prim() << endl;
}

C++: //include
//------------------------------------------
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <limits>
using namespace std;

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,a,b) for(int i=(a);i>(b);i--)
#define REP(i,n)  FOR(i,0,n)

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//debug
//-------------------------------------------
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define INF std::numeric_limits<int>::max()

//typedef
//-------------------------------------------
typedef long long ll;

int n;
int g[101][101];

int prim(int used, int sum) {
    if (used == 0) return sum;

    int tmp = INF;
    int use = -1;
    REP(i, n) {
        if ((used>>i)&1) {
            continue;
        }
        REP(j, n) {
            if (((used>>j)&1) && g[i][j] < tmp) {
                tmp = g[i][j];
                use = j;
            }
        }
    }
    return prim(used&(~(1<<use)), sum + tmp);
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> n;
    REP(i, n) REP(j, n) {
        cin >> g[i][j];
        if (g[i][j] < 0) g[i][j] = INF;
    }
    int used = (1<<n) - 1;
    cout << prim(used&(~1), 0) << endl;

    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;

const int MAX = 105;
const int INF = 1000000007;
const int white = 0;
const int gray = 1;
const int black = 2;

int n, M[MAX][MAX];

int prim() {
  int u, minv;
  int d[MAX], p[MAX], color[MAX];
  rep(i, n) {
    d[i] = INF;
    p[i] = -1;
    color[i] = white;
  }
  d[0] = 0;
  while (1) {
    minv = INF;
    u = -1;
    rep(i, n) {
      if (minv > d[i] && color[i] != black) {
        u = i;
        minv = d[i];
      }
    }
    if (u == -1) break;
    color[u] = black;
    rep(v, n) {
      if (color[v] != black && M[u][v] != INF) {
        if (d[v] > M[u][v]) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = gray;
        }
      }
    }
  }
  int sum = 0;
  rep(i, n) {
    if (p[i] != -1) {
      sum += M[i][p[i]];
    }
  }
  return sum;
}

int main() {
  cin >> n;
  rep(i, n) {
    rep(j, n) {
      int e;
      cin >> e;
      M[i][j] = (e == -1) ? INF : e;
    }
  }
  cout << prim() << endl;

  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
constexpr int INF = (1 << 30) - 1;

int n;
int dist[105][105];

int prim()
{
    int d[105];
    bool flag[105] = {};
    int cnt = 0, sum = 0;
    fill_n(d, 105, INF);
    d[0] = 0;
    
    while ( true ) {
        int u = -1;
        for ( int i = 0; i < n; i++ ) {
            if ( !flag[i] && (u == -1 || d[u] > d[i] ) ) u = i;
        }
        
        flag[u] = true;
        sum += d[u];
        cnt++;
        if ( cnt == n ) break;

        for ( int i = 0; i < n; i++ ) {
            if ( !flag[i] && dist[u][i] != -1 && d[i] > dist[u][i] ) {
                d[i] = dist[u][i];
            }
        }
    }

    return ( sum );
}

int main()
{
    cin >> n;
    for ( int i = 0; i < n; i++ ) {
        for ( int j = 0; j < n; j++ ) {
            cin >> dist[i][j];
        }
    }
    cout << prim() << endl;
    return ( 0 );
}

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

#define WHITE 1
#define BLACK 2
#define GRAY 3
#define M 100
#define INFTY (1<<21)

int n,color[M],A[M][M];

int prim(){
  int d[M],p[M],u = -1,min;

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

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

    color[u] = BLACK;

    for(int i = 0;i < n;i++){
      if(color[i] != BLACK && A[u][i] != -1){
        if(A[u][i] < d[i]){
          d[i] = A[u][i];
          p[i] = u;
          color[i] = GRAY;
        }
      }
    }
  }

  int re = 0;
  for(int i = 0;i < n;i++){
    if(p[i] != -1) re += A[i][p[i]];
  }

  return re;
}

int main(){

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

  cout << prim() << endl;

  return 0;
}