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;

#define INF 1000000

int main(){
  long n;
  long G[100][100],d[100];
  long minc,mi,i;
  bool flgs[i];
  long suma=0;
  
  cin >> n;
  for (i=0;i<n;i++){
    for (long j=0;j<n;j++){
      cin >> G[i][j];
      if (G[i][j]==-1) G[i][j]=INF;
    }
    d[i]=INF;
    flgs[i]=false;
  }

  d[0]=0;
  while (true){
    minc=INF;
    for (i=0;i<n;i++){
      if (!flgs[i]&&d[i]<minc){
	minc=d[i];
	mi=i;
      }
    }
    if (minc==INF) break;
    flgs[mi]=true;
    for (i=0;i<n;i++){
      if (G[mi][i]==INF)continue;
      if (!flgs[i]&&d[i]>G[mi][i]){
	d[i]=G[mi][i];
	//cout << d[i];
      }
    }
  }
  
  for (i=0;i<n;i++){
    suma+=d[i];
  }
  cout << suma<<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(true)
    {
        minv = INFTY;
        u = -1;
        for (int i = 0; i < n; i++)
        {
            if (color[i] != BLACK && d[i] < minv)
            {
                minv = d[i];
                u = i;
            }
        }
        if (u == -1) break;
        color[u] = BLACK;
        for (int v = 0; v < n; v++)
        {
            if (color[v] != BLACK && M[u][v] != INFTY)
            {
                if ( M[u][v] < d[v])
                {
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    int s = 0;
    for (int i = 0; i < n; i++)
    {
        if (p[i] != -1)
        {
            s += M[i][p[i]];
        }
    }
    return s;
}

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 <vector>
#include <climits>
#include <set>

class Graph
{
 private:
  std::vector<std::vector<long> > weight_;
 public:
  Graph(const unsigned long node_num){};


  void insert_weight(const std::vector<long> weight){
    weight_.push_back(weight);
  };


  void display()
  {
    for(unsigned long i = 0, imax = weight_.size(); i < imax; ++i)
    {
      for(unsigned long j = 0, jmax = weight_[i].size(); j < jmax; ++j)
      {
        std::cout << " " << weight_[i][j];
      }
      std::cout << std::endl;
    }
  };


  unsigned long MinimumSpanningTree()
  {
    if(weight_.size() <= 1) return 0;

    struct Edge
    {
      long value_;
      long start_node_;
      long end_node_;
      Edge(long value = LONG_MAX, long sn = -1, long en = -1) : value_(value), start_node_(sn), end_node_(en){};
      void change(long value, long sn, long en)
      {
        value_ = value; start_node_ = sn, end_node_ = en;
      };
      void display()
      {
        std::cout << value_ << " : " << start_node_ << " : " << end_node_ << std::endl;
      };
    };

    unsigned long minimum_spanning_value = 0;

    std::set<long> connect_node; connect_node.insert(0);
    std::set<long> noconnect_node;

    for(unsigned long node_i = 1, imax = weight_.size(); node_i < imax; ++node_i)
      noconnect_node.insert(node_i);

    while(!noconnect_node.empty())
    {
      Edge min_edge;
      for(std::set<long>::iterator it = connect_node.begin(); it != connect_node.end(); ++it)
      {
        for(std::set<long>::iterator noit = noconnect_node.begin(); noit != noconnect_node.end(); ++noit)
        {
          if(weight_[*it][*noit] == -1) continue;
          if(weight_[*it][*noit] < min_edge.value_)
          {
            min_edge.value_ = weight_[*it][*noit];
            min_edge.change(weight_[*it][*noit], *it, *noit);
          }
        }
      }
      connect_node.insert(min_edge.end_node_);
      noconnect_node.erase(min_edge.end_node_);
      minimum_spanning_value += min_edge.value_;
    }

    return minimum_spanning_value;
  };//   unsigned long MinimumSpanningTree()
 protected:
};//    class Graph

int main()
{

  unsigned long node_num = 0;
  std::cin >> node_num; std::cin.ignore();

  Graph graph(node_num);

  for(unsigned long node_i = 0; node_i < node_num; ++node_i)
  {
    std::vector<long> weight(node_num, -1);
    for(unsigned long weight_i = 0, weight_num = node_num; weight_i < weight_num; ++weight_i)
    {
      std::cin >> weight[weight_i]; std::cin.ignore();
    }
    graph.insert_weight(weight);
  }

  std::cout << graph.MinimumSpanningTree() << std::endl;

  return 0;
};//    main()