#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define mod 1000000007
#define IOS ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
const int maxn = 1e3 + 10;
int vis[maxn], d[maxn], mat[maxn][maxn];
int n, cur, ans = 0;
int main() {
scanf("%d", &n);
for (int i = 1;i <= n;++i) {
for (int j = 1;j <= n;++j) {
scanf("%d", &mat[i][j]);
}
}
memset(vis, 0, sizeof(vis));
memset(d, INF, sizeof(d));
d[1] = 0;
for (int i = 1;i <= n;++i) {
cur = -1;
for (int j = 1;j <= n;++j) {
if (!vis[j] && (cur == -1 || d[cur] > d[j])) {
cur = j;
}
}
ans += d[cur];
vis[cur] = 1;
for (int j = 1;j <= n;++j) {
if (!vis[j] && d[j] > mat[cur][j]) {
d[j] = mat[cur][j];
}
}
}
printf("%d", ans);
return 0;
}