#line 1 "main.cpp" #include #include #include using namespace std; constexpr int INF = 1 << 30; void solve() { int n, m; cin >> n >> m; auto xss = vector(n, vector(n, 0)); for (auto& xs : xss) { for (auto& x : xs) cin >> x; } int ans = INF; for (int b = 0; b < 4; ++b) { auto yss = xss; int cost = 0, rem = m; if (b & 1) { --rem; for (int i = 0; i < n; ++i) { cost += yss[i][i]; yss[i][i] = 0; } } if (b & 2) { --rem; for (int i = 0; i < n; ++i) { cost += yss[i][n - 1 - i]; yss[i][n - 1 - i] = 0; } } for (int p = 0; p < (1 << n); ++p) { int c = cost; int r = rem - __builtin_popcount(p); if (r > n) continue; for (int i = 0; i < n; ++i) { if ((~p >> i) & 1) continue; for (int j = 0; j < n; ++j) c += yss[i][j]; } vector cs(n, 0); for (int j = 0; j < n; ++j) { for (int i = 0; i < n; ++i) { if ((~p >> i) & 1) cs[j] += yss[i][j]; } } sort(cs.begin(), cs.end()); for (int i = 0; i < r; ++i) c += cs[i]; ans = min(ans, c); } } cout << ans << "\n"; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); solve(); return 0; }