#include using namespace std; template using vec = vector; template using vvec = vec>; template using lqueue = priority_queue, greater>; template using gqueue = priority_queue, less>; int main() { int n, m; cin >> n >> m; vvec a(n, vec(n)); for (auto &e1 : a) for (auto &e2 : e1) cin >> e2; int ans = INT_MAX; for (int i = 0; i < (1 << (n + 2)); ++i) { vvec used(n, vec(n, false)); vec col(n, 0); for (int j = 0; j < n; ++j) { for (int k = 0; k < n; ++k) { col[k] += a[j][k]; } } int count = 0, cost = 0; for (int j = 0; j < n; ++j) { if (!(i & (1 << j))) continue; for (int k = 0; k < n; ++k) { used[j][k] = true; cost += a[j][k]; col[k] -= a[j][k]; } ++count; } int dia = i >> n; if (dia & (1 << 0)) { for (int j = 0; j < n; ++j) { if (used[j][j]) continue; used[j][j] = true; cost += a[j][j]; col[j] -= a[j][j]; } ++count; } if (dia & (1 << 1)) { for (int j = 0; j < n; ++j) { int y = j, x = n - j - 1; if (used[y][x]) continue; used[y][x] = true; cost += a[y][x]; col[x] -= a[y][x]; } ++count; } sort(begin(col), end(col)); for (int j = 1; j < n; ++j) { col[j] += col[j - 1]; } if (m <= count) { ans = min(ans, cost); } else if (m - count - 1 < n) { ans = min(ans, cost + col[m - count - 1]); } } cout << ans << endl; }