#include <bits/stdc++.h>
using namespace std;
int a[5005];
struct edge
{
int x, y, z;
bool operator < (const edge __) const
{
return z < __.z;
}
}edge[200005];
int find(int x)
{
if (a[x] != x)
{
a[x] = find(a[x]);
}
return a[x];
}
int n, m;
int main()
{
scanf("%d%d", &n, &m);
int cnt = 0;
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j <= n; j ++)
{
int x;
cin >> x;
if (x > m)
{
edge[++ cnt] = {i, j, m};
}
else if (x > 0)
{
edge[++ cnt] = {i, j, x};
}
}
}
sort(edge + 1, edge + 1 + cnt);
for (int i = 1; i <= n; i ++)
{
a[i] = i;
}
long long res = 0, ans = 0;
for (int i = 1; i <= cnt; i ++)
{
int _x = edge[i].x, _y = edge[i].y, _z = edge[i].z;
int a1 = find(_x), b1 = find(_y);
if (a1 == b1)
{
continue;
}
a[a1] = b1;
res ++;
ans += _z;
}
cout << ans + m;
return 0;
}
注意到ki,j可能大于A了