#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define ln cout<<'\n'
void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<' ';pr(b...);}
void chmin(int& x, int y) { x = min(x, y);}


void solve() {
	int n, k; cin >> n >> k;
	int a[810][810];
	rep(i, n) rep(j, n) cin >> a[i][j];
	int l = -1, u = 1001001001;
	int L = k*k/2+1;

	while(l + 1 < u) {
		bool ok = false;
		int mid = (l + u) / 2;
		{
			int s[810][810];
			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 < L) ok = true;
			}
		}
		if (ok) u = mid; else l = mid;
	}
	cout << u << endl;
}

int main(){
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	solve();
	return 0;
}

#include <algorithm>
#include <iostream>

using namespace std;

const int N = 800, A = 1000000000;

int aa[N + 1][N + 1], pp[N + 1][N + 1];

int main() {
	int n, k; cin >> n >> k;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			cin >> aa[i][j];
	int lower = -1, upper = A;
	while (upper - lower > 1) {
		int a = (lower + upper) / 2;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				pp[i][j] = (aa[i][j] <= a) + pp[i - 1][j] + pp[i][j - 1] - pp[i - 1][j - 1];
		bool yes = false;
		for (int i = k; i <= n; i++)
			for (int j = k; j <= n; j++)
				if (pp[i][j] - pp[i - k][j] - pp[i][j - k] + pp[i - k][j - k] >= (k * k + 1) / 2)
					yes = true;
		if (yes)
			upper = a;
		else
			lower = a;
	}
	cout << upper << '\n';
	return 0;
}
