#include <bits/stdc++.h>
using namespace std;

const int nax = 805;
int n, k, a[nax][nax];
int sum[nax][nax];

int main() {
    cin >> n >> k;
    for(int i = 1; i <= n; i += 1)
        for(int j = 1; j <= n; j += 1)
            cin >> a[i][j];

    int L = 0, R = 1e9;
    while(L < R) {
        int M = (L + R + 1) / 2;
        for(int i = 1; i <= n; i += 1)
            for(int j = 1; j <= n; j += 1) {
                sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + (a[i][j] >= M ? 1 : -1);
        }
        int ok = 1;
        for(int i = k; i <= n; i += 1) {
            for(int j = k; j <= n; j += 1) {
                if(sum[i][j] - sum[i - k][j] - sum[i][j - k] + sum[i - k][j - k] <= 0)
                    ok = 0;
            }
        }
        if(ok) L = M;
        else R = M - 1;
    }
    cout << L << endl;
}

//设x是所求最小的中位数，若M <= x, 所有的k * k的sum矩阵值为正;若M > x，所有的k * k的sum矩阵非正。所以二分求最大值即可
#include<bits/stdc++.h>

using namespace std;

#define  GODSPEED       ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;cout<<setprecision(15);
#define  f              first
#define  s              second
#define  newl           cout<<"\n";
#define  pb             push_back
#define  mset(a,x)      memset(a,x,sizeof(a))
#define  debv(a)        for(auto it: a)cout<<it<<" ";newl;
#define  deb1(a)        cout<<a<<"\n";
#define  deb2(a,b)      cout<<a<<" "<<b<<"\n";
#define  deb3(a,b,c)    cout<<a<<" "<<b<<" "<<c<<"\n";
#define  deb4(a,b,c,d)  cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n";
#define  uniq(a)        a.resize(unique(a.begin(), a.end()) - a.begin());
#define  all(a)         a.begin(),a.end()

typedef  long long             ll;
typedef  long double           ld;
typedef  pair<ll, ll>           pll;
typedef  vector<ll>            vll;
typedef  vector<pll>           vpll;

const ll   N     =  2e3 + 5;
const ll   mod   =  1e9 + 7;
const ll   INF   =  1e18;
const int  INFi  =  0x7f7f7f7f;
const ll   LEVEL =  log2(N) + 1;

int n, k, a[805][805], b[805][805], x[805][805], y[805][805];

int f(int mid) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			b[i][j] = a[i][j] <= mid;
		}
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			x[i][j] = y[i][j] = b[i][j];
			if (i) y[i][j] += y[i - 1][j];
			if (j) x[i][j] += x[i][j - 1];
		}
	}
	int ct = 0;
	for (int i = 0; i < k; i++) {
		for (int j = 0; j < k; j++) {
			ct += b[i][j];
		}
	}
	for (int i = 0; i <= n - k; i++) {
		if (i % 2 == 0) {
			if (i) {
				// for (int j = 0; j < k; j++)
				ct -= x[i - 1][k - 1];
				ct += x[i - 1 + k][k - 1];
			}
			// deb1(ct)
			if (ct >= (k * k + 1) / 2) return 1;
			for (int j = 0; j <= n - k - 1; j++) {
				// for (int ii = i; ii < i + k; ii++)
				ct -= y[i + k - 1][j];
				if (i) ct += y[i - 1][j];
				ct += y[i + k - 1][j + k];
				if (i) ct -= y[i - 1][j + k];
				// deb1(ct)
				if (ct >= (k * k + 1) / 2) return 1;
			}
		} else {
			// for (int j = n - 1; j > n - k - 1; j--)
			ct -= x[i - 1][n - 1];
			if (n - k) ct += x[i - 1][n - k - 1];
			ct += x[i + k - 1][n - 1];
			if (n - k) ct -= x[i + k - 1][n - k - 1];
			// deb1(ct)
			if (ct >= (k * k + 1) / 2) return 1;
			for (int j = n - 1; j >= k; j--) {
				// for (int ii = i; ii < i + k; ii++)
				ct -= y[i + k - 1][j];
				if (i) ct += y[i - 1][j];
				ct += y[i + k - 1][j - k];
				if (i) ct -= y[i - 1][j - k];
				// deb1(ct)
				if (ct >= (k * k + 1) / 2) return 1;
			}
		}
	}
	return 0;
}

void solve() {
	cin >> n >> k;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> a[i][j];
		}
	}
	ll l = 0, r = 1e9, ans;
	while (l <= r) {
		ll mid = (l + r) / 2;
		if (f(mid)) {
			ans = mid;
			r = mid - 1;
		} else l = mid + 1;
	}
	deb1(ans)
}

int main() {
	GODSPEED;
	int test = 1;
	// cin >> test;
	for (int i = 1; i <= test; i++) {
		solve();
	}
}