package com.liang.poj;
import java.util.Scanner;
public class Test1258Kruskal {
static int n = 0;
static int[][] cin = null;
static int[][] result = null;
static int index = 0;
static int[] father = null;
static int[] rank = null;
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
n = scan.nextInt();
cin = new int[n][n];
father = new int[n];
result = new int[n * n - n][3];
rank = new int[n];
for (int i = 0; i < cin.length; i++) {
for (int j = 0; j < cin[i].length; j++) {
cin[i][j] = scan.nextInt();
}
}
for (int i = 0; i < cin.length; i++) {
for (int j = 0; j < cin[i].length; j++) {
if (cin[i][j] != 0) {
result[index][0] = i;
result[index][1] = j;
result[index][2] = cin[i][j];
index++;
}
}
}
peixu();
kruskal(0);
}
public static void kruskal(int count) {
int sum = 0;
makeSet();
for (int i = 0; i < result.length; i++) {
int a = result[i][0];
int b = result[i][1];
if (merge(a, b)) {
sum += result[i][2];
count++;
}
if(count == n-1) {
break;
}
}
System.out.println(sum);
}
public static void makeSet() {
for (int i = 0; i < father.length; i++) {
father[i] = i;
rank[i] = 0;
}
}
public static int findFather(int x) {
if (x != father[x]) {
x = findFather(father[x]);
}
return x;
}
public static boolean merge(int a, int b) {
int fa = findFather(a);
int fb = findFather(b);
if (fa == fb) {
return false;
} else {
if (rank[fa] >= rank[fb]) {
father[fb] = fa;
rank[fa]++;
} else {
father[fa] = fb;
rank[fb]++;
}
return true;
}
}
public static void peixu() { // 对权值进行排序
for (int i = 0; i < result.length; i++) {
if (result[i][2] == 0) {
continue;
}
for (int j = i + 1; j < result.length; j++) {
if (result[j][2] == 0) {
continue;
}
if (result[i][2] > result[j][2]) {
jiaohuan(i, j, 0);
jiaohuan(i, j, 1);
jiaohuan(i, j, 2);
}
}
}
}
public static void jiaohuan(int i, int j, int cos) {
int x = 0;
x = result[i][cos];
result[i][cos] = result[j][cos];
result[j][cos] = x;
}
}