#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0, length = n; i < length; i++)
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ep emplace
#define epb emplace_back
#define scll static_cast<long long> 
#define sz(x) static_cast<int>((x).size())
#define pfll(x) printf("%lld\n", x)
#define ci(x) cin >> x
#define ci2(x, y) cin >> x >> y
#define ci3(x, y, z) cin >> x >> y >> z
#define ci4(w, x, y, z) cin >> w >> x >> y >> z
#define co(x) cout << x << endl
#define co2(x, y) cout << x << " " << y << endl
#define co3(x, y, z) cout << x << " " << y << " " << z << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> PQG;
typedef priority_queue<P> PQP;
typedef priority_queue<P, vector<P>, greater<P>> PQPG;

const int MAX_N = 2e5, MOD = 1e9 + 7, INF = 1e9;
int n, k, a[800][800];

int comp(int x) {
    int sum[n + 1][n + 1];
    rep(i, n + 1) sum[0][i] = 0;
    rep(i, n + 1) sum[i][0] = 0;
    rep(i, n) rep(j, n) sum[i + 1][j + 1] = sum[i][j + 1] + (a[i][j] >= x);
    rep(i, n - k + 1) {
        int tmp = 0;
        rep(j, k) tmp += sum[i + k][j] - sum[i][j];
        rep(j, n - k + 1) {
            tmp += sum[i + k][j + k] - sum[i][j + k] -
                sum[i + k][j] + sum[i][j];
            if (tmp < k * k / 2 + 1) return false;
        }
    }
    return true;
}
                
int main() {
    ci2(n, k);
    rep(i, n) rep(j, n) ci(a[i][j]);
    int l = 0, r = 1e9 + 1;
    while (r - l > 1) {
        int mid = (l + r) / 2;
        ((comp(mid)) ? l : r) = mid;
    }
    co(l);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<n;i++)
const int INF = 1e9;

int n,k;
int a[805][805], s[805][805];

int binary_search(int key) {
  auto check = [&](int mid) {
    REP(i,n) REP(j,n) s[i+1][j+1] = a[i][j] > mid ? 1 : 0;
    REP(i,n+1) REP(j,n) s[i][j+1] += s[i][j];
    REP(i,n) REP(j,n+1) s[i+1][j] += s[i][j];

    REP(i,n-k+1) REP(j,n-k+1) {
      int now = s[i+k][j+k];
      now -= s[i][j+k];
      now -= s[i+k][j];
      now += s[i][j];

      if (now < key) return true;
    }
    return false;
  };

  int ok = INF;
  int ng = -1;
  while(abs(ok-ng) > 1) {
    int mid = (ok+ng)/2;
    check(mid) ? ok = mid : ng = mid;
  }

  return ok;
}

int main() {
  cin >> n >> k;
  REP(i,n) REP(j,n) cin >> a[i][j];

  int med = k * k / 2 + 1;
  cout << binary_search(med) << endl;
  return 0;
}