import java.io.BufferedInputStream;
import java.util.Scanner;
public class Prim {
static int[][] arr;
static boolean flag[]; //用来标记节点i是否被覆盖
static int n;
static int sum;
static final int maxInt = Integer.MAX_VALUE;
public static void main(String[] args) {
Scanner s = new Scanner(new BufferedInputStream(System.in));
while(s.hasNextInt()){
n = s.nextInt();
arr = new int[n+1][n+1];
flag = new boolean[n+1];
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
arr[i][j] = s.nextInt(); //从下标1开始存储
sum = 0;
flag[1] = true; //选取第一个节点
for(int k=1; k<n; k++){ //循环n-1次
int min = maxInt,min_i = 0;
for(int i=1; i<=n; i++){
if( !flag[i] && arr[1][i] < min){
min = arr[1][i];
min_i = i;
}
}
System.out.println(" min_i:"+min_i);
flag[min_i] = true; // 覆盖节点
for(int i=1; i<=n; i++){ //更新未覆盖节点的距离,每加入一个点就更新
if( !flag[i] && arr[1][i] > arr[min_i][i])
arr[1][i] = arr[min_i][i];
System.out.print("arr[1]["+i+"]"+arr[1][i]+" ");
}
System.out.println();
sum += arr[1][min_i];//加上权值
}
System.out.println(sum);
}
}
}