#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-9;
const int inf = (1 << 30) - 1;
const long long inf64 = (1LL << 62) - 1;
const long double pi = acos(-1);
template <class T>
T sqr(T x) {
  return x * x;
}
template <class T>
T abs(T x) {
  return x < 0 ? -x : x;
}
int main() {
  ios_base::sync_with_stdio(0);
  int n, k;
  cin >> n >> k;
  vector<vector<int>> a(n, vector<int>(n, -1));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> a[i][j];
    }
  }
  vector<vector<vector<int>>> d(
      4, vector<vector<int>>(n, vector<int>(n, inf / 5)));
  vector<vector<vector<int>>> v(4, vector<vector<int>>(n, vector<int>(n, n)));
  for (int v1 = 0; v1 < n; ++v1) {
    for (int v2 = 0; v2 < n; ++v2) {
      vector<pair<int, int>> dis;
      for (int v3 = 1; v3 < n; ++v3) {
        if (v3 != v1 && v3 != v2) {
          dis.push_back(make_pair(a[v1][v3] + a[v3][v2], v3));
        }
      }
      sort(dis.begin(), dis.end());
      for (int i = 0; i < min(4, ((int)(dis).size())); ++i) {
        d[i][v1][v2] = dis[i].first;
        v[i][v1][v2] = dis[i].second;
      }
    }
  }
  int res = inf;
  if (k == 2) {
    res = d[0][0][0];
  } else if (k == 4) {
    for (int v1 = 0; v1 < n; ++v1) {
      res = min(res, d[0][0][v1] + d[0][v1][0]);
    }
  } else if (k == 6) {
    for (int v1 = 0; v1 < n; ++v1) {
      for (int v2 = 0; v2 < n; ++v2) {
        int ind1 = 0;
        if (v[0][0][v1] == v2) {
          ind1 = 1;
        }
        int ind2 = 0;
        if (v[0][v2][0] == v1) {
          ind2 = 1;
        }
        res = min(res, d[ind1][0][v1] + d[0][v1][v2] + d[ind2][v2][0]);
      }
    }
  } else if (k == 8) {
    for (int v1 = 0; v1 < n; ++v1) {
      for (int v2 = 0; v2 < n; ++v2) {
        for (int v3 = 0; v3 < n; ++v3) {
          int ind1 = 0;
          while (v[ind1][0][v1] == v2 || v[ind1][0][v1] == v3) {
            ++ind1;
          }
          int ind2 = 0;
          if (v[0][v1][v2] == v3) {
            ind2 = 1;
          }
          int ind3 = 0;
          if (v[0][v2][v3] == v1) {
            ind3 = 1;
          }
          int ind4 = 0;
          while (v[ind4][v3][0] == v1 || v[ind4][v3][0] == v2) {
            ++ind4;
          }
          res = min(res, d[ind1][0][v1] + d[ind2][v1][v2] + d[ind3][v2][v3] +
                             d[ind4][v3][0]);
        }
      }
    }
  } else {
    vector<int> cnt(n + 1, 0);
    int ind1, ind2, ind3, ind4, ind5;
    for (int v1 = 0; v1 < n; ++v1) {
      ++cnt[v1];
      for (int v2 = 0; v2 < n; ++v2) {
        ++cnt[v2];
        for (int v3 = 0; v3 < n; ++v3) {
          ++cnt[v3];
          for (int v4 = 0; v4 < n; ++v4) {
            ++cnt[v4];
            ind1 = 0;
            ind2 = 0;
            ind3 = 0;
            ind4 = 0;
            ind5 = 0;
            while (cnt[v[ind1][0][v1]]) {
              ++ind1;
            }
            while (cnt[v[ind2][v1][v2]]) {
              ++ind2;
            }
            while (cnt[v[ind3][v2][v3]]) {
              ++ind3;
            }
            while (cnt[v[ind4][v3][v4]]) {
              ++ind4;
            }
            while (cnt[v[ind5][v4][0]]) {
              ++ind5;
            }
            res = min(res, d[ind1][0][v1] + d[ind2][v1][v2] + d[ind3][v2][v3] +
                               d[ind4][v3][v4] + d[ind5][v4][0]);
            --cnt[v4];
          }
          --cnt[v3];
        }
        --cnt[v2];
      }
      --cnt[v1];
    }
  }
  cout << res << "\n";
  return 0;
}
