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>

using namespace std;

static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GREY = 1;
static const int 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; i < n; i++) {
      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;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  cout << prim() << endl;

  return 0;
}

C++: #include <iostream>
#include <stack>
using namespace std;
static const int MAX_V = 100;
static const int INF = 10000000;
int cost[MAX_V][MAX_V];
bool wasUsed[MAX_V];
int mincost[MAX_V];
//n???????????°
int n;

int prim(){
    for(int i=0;i<n;i++){
        wasUsed[i] = false;
        mincost[i] = INF;
    }
    mincost[0] = 0;
    int res = 0;
    while(true){
        int v = -1;
        for(int u =0;u<n;u++){
            if(!wasUsed[u] && (v==-1||mincost[u] < mincost[v]))
               v = u;
        }
        if(v==-1) break;
        wasUsed[v] = true;
        res += mincost[v];
        
        for(int u=0;u<n;u++)
            mincost[u] = min(mincost[u],cost[v][u]);
    }
    return res;
}

int main(int argc, const char * argv[]) {
    cin >> n;
    
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++){
            int a;
            cin >> a;
            if(a == -1) a = INF;
            cost[i][j] = a;
        }
    
    cout << prim() << endl;
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

static const int MAX = 100;
static const int INF = (1<<21);
static const int WHI = 0;
static const int GRA = 1;
static const int BLA = 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] = INF;
    p[i] = -1;
    color[i] = WHI;
  }
  d[0] = 0;
  
  while (1) {
    minv = INF;
    u = -1;
    for ( int i = 0; i < n ; ++i) {
      if ( minv > d[i] && color[i] != BLA ) {
	u = i;
	minv = d[i];
      }
    }
    if ( u == -1 ) {
      break; 
    }
    color[u] = BLA;
    
    for ( int v = 0; v < n ; ++v) {
      if ( color[v] != BLA && m[u][v] != INF) {
	if ( m[u][v] < d[v] ) {
	  d[v] = m[u][v];
	  p[v] = u;
	  color[v] = GRA;
	}
      }
    }
  }
  int sum = 0;
  for ( int i = 0; i < n ; ++i) {
    if ( p[i] != -1 ) {
      sum += m[i][p[i]];
    }
  }
  return sum;
}

  
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] = INF;
      } else {
	m[i][j] = e;
      }
    }
  }

  cout << prim() << endl;

  return (0);
}

C++: #include<iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int 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; // uに対しての辺がなかった場合
        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;
            M[i][j] = (e== -1) ? INFTY : e;
        }
    }
    cout << prim() << endl;
    return 0;
}

C++: #include<iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int 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;
			M[i][j] = (e == -1) ? INFTY : e;
		}
	}
	cout << prim() << endl;
    return 0;
}