package week0315_0319;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class MST2_PrimTest {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
int[][] adjMatrix = new int[N][N];
boolean[] visited = new boolean[N];
int[] minEdge = new int[N];
for(int i = 0 ; i < N ; i++) {
st = new StringTokenizer(br.readLine());
for(int j = 0 ; j < N ; j++) {
adjMatrix[i][j] = Integer.parseInt(st.nextToken());
}
minEdge[i] = Integer.MAX_VALUE;
}
int result = 0;
minEdge[0] = 0;
for(int i = 0 ; i < N ; i++) {
int min = Integer.MAX_VALUE;
int minVertex = 0;
// 신장 트리에 연결되지 않은 접점중 minEdge비용이 최소인 정점
for(int j = 0 ; j < N ; j++) {
if(!visited[j] && min > minEdge[j]) {
min = minEdge[j];
minVertex = j;
}
}
result += min;
visited[minVertex] = true;
for(int j = 0 ; j < N ; j++) {
if(!visited[j] && adjMatrix[minVertex][j] != 0 && minEdge[j] > adjMatrix[minVertex][j]) {
minEdge[j] = adjMatrix[minVertex][j];
}
}
}
}
}