//
// Created by Anton Gorokhov
//

#ifdef lolipop
#define _GLIBCXX_DEBUG
#endif

#include <iostream>
#include <cstddef>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <ctime>
#include <unordered_map>
#include <random>
#include <iomanip>
#include <cmath>
#include <queue>
#include <unordered_set>
#include <cassert>
#include <bitset>
#include <deque>
#include <utility>

#define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ld double

using namespace std;

mt19937 rnd(time(nullptr));

inline void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

const int inf = 2e9 + 123;

void solve() {
    int n, k;
    cin >> n >> k;
    vector<vector<int> > v(n, vector<int>(n, 0));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> v[i][j];
        }
    }
    int L = -1, R = inf;
    while (L + 1 < R) {
        int M = (L + R) / 2;
        vector<vector<int> > val(n + 1, vector<int>(n + 1, 0));
        auto sum = [&] (int x1, int y1, int x2, int y2) {
            x2++, y2++;
            return val[x2][y2] + val[x1][y1] - val[x1][y2] - val[x2][y1];
        };
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                val[i + 1][j + 1] = val[i][j + 1] + val[i + 1][j] - val[i][j] + (v[i][j] <= M);
            }
        }
        bool ok = false;
        for (int i = 0; i + k - 1 < n; ++i) {
            for (int j = 0; j + k - 1 < n; ++j) {
                if (sum(i, j, i + k - 1, j + k - 1) >= (k * k + 1) / 2) {
                    ok = true;
                    break;
                }
            }
            if (ok) break;
        }
        if (ok) {
            R = M;
        } else {
            L = M;
        }
    }
    cout << R << '\n';
}

signed main() {
#ifdef lolipop
    freopen("input.txt", "r", stdin);
#else
    fastio();
#endif
    int T = 1;
//    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define RSORT(x) sort(x.rbegin(), x.rend())
#define REVERSE(x) reverse(ALL(x))
#define MAX(x) *max_element(ALL(x))
#define MAXI(x) max_element(ALL(x)) - x.begin()
#define SUM(x) accumulate(ALL(x), 0ll)
#define COUNT(x, y) count(ALL(x), y);
#define ANS cout << ans << "\n"
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Yes cout << "Yes\n";
#define No cout << "No\n";
#define init() \
  cin.tie(0);  \
  ios::sync_with_stdio(false)
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v)                       \
  cerr << "[debugV] " << #v << ":";     \
  rep(z, v.size()) cerr << " " << v[z]; \
  cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;

template <typename T>
inline istream& operator>>(istream& i, vector<T>& v) {
  rep(j, v.size()) i >> v[j];
  return i;
}

template <typename T1, typename T2>
inline istream& operator>>(istream& i, pair<T1, T2>& v) {
  return i >> v.first >> v.second;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
  return to_string(x).size();
}

ll gcd(ll x, ll y) {
  return y ? gcd(y, x % y) : x;
}

ll lcm(ll a, ll b) {
  return a / gcd(a, b) * b;
}

template <class T>
void chmax(T& a, const T& b) {
  if (a < b) a = b;
}

template <class T>
void chmin(T& a, const T& b) {
  if (b < a) a = b;
}

signed main() {
  init();
  ll N, K;
  cin >> N >> K;
  vvll A(N, vll(N));
  cin >> A;

  ll l = 0, r = INF;
  while (r - l > 1) {
    ll c = (l + r) / 2;
    vvll R(N, vll(N));
    rep(i, N) rep(j, N) {
      if (c <= A[i][j]) R[i][j] += 1;
      if (i != 0) R[i][j] += R[i - 1][j];
      if (j != 0) R[i][j] += R[i][j - 1];
      if (i != 0 and j != 0) R[i][j] -= R[i - 1][j - 1];
    }
    ll cnt = INF;
    rep(i, N - (K - 1)) rep(j, N - (K - 1)) {
      ll count = R[i + K - 1][j + K - 1];
      if (i != 0) count -= R[i - 1][j + K - 1];
      if (j != 0) count -= R[i + K - 1][j - 1];
      if (i != 0 and j != 0) count += R[i - 1][j - 1];
      chmin(cnt, count);
    }
    if (cnt < (K * K) / 2 + 1)
      r = c;
    else
      l = c;
  }
  COUT(l);
  return 0;
}