#include #define rep(i, n) for (int i = 0; i < (n); i++) #define rrep(i, n) for (int i = (n - 1); i >= 0; i--) #define ALL(v) v.begin(), v.end() using namespace std; using P = pair; typedef long long ll; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } const int dx[4] = {1, 0, -1, 0}; const int dy[4] = {0, 1, 0, -1}; int main() { int n, m; cin >> n >> m; ll ans = (1LL << 60); vector> A(n, vector(n)); rep(i, n) rep(j, n) cin >> A[i][j]; for (int bit = 1; bit < (1 << (2 * n + 2)); bit++){ int d = __builtin_popcount(bit); vector> cnt(n, vector(n, 0)); if(d != m)continue; ll res = 0; rep(i, 2 *n +2){ if(bit & (1 << i)){ if(i < n){ rep(j, n){ if(cnt[i][j] > 1)continue; res += A[i][j]; cnt[i][j]++; } }else if(i >= n && i < 2 * n){ rep(j, n){ if(cnt[j][i-n] > 0)continue; res += A[j][i - n]; cnt[j][i- n]++; } }else{ if(i == 2 * n){ rep(j, n){ if(cnt[j][j] > 0)continue; res += A[j][j]; cnt[j][j]++; } }else{ rep(j, n){ if(cnt[j][n-1-j] > 0)continue; res += A[j][n -1- j]; cnt[j][n -1- j]++; } } } } } chmin(ans, res); } cout << ans << endl; }