#include<bits/stdc++.h>
using namespace std;
# define ll long long
# define read read1<ll>()
# define Type template<typename T>
Type T read1(){
	T t=0;
	char k;
	bool vis=0;
	do (k=getchar())=='-'&&(vis=1);while('0'>k||k>'9');
	while('0'<=k&&k<='9')t=(t<<3)+(t<<1)+(k^'0'),k=getchar();
	return vis?-t:t;
}
# define fre(k) freopen(k".in","r",stdin);freopen(k".out","w",stdout)
int s,k,a[805][805],b[805][805];
vector<int>ve;
bool judge(int x){
	for(int i=1;i<=s;++i)
		for(int j=1;j<=s;++j)
			b[i][j]=b[i-1][j]+b[i][j-1]-b[i-1][j-1]+(a[i][j]>x);
	for(int i=k;i<=s;++i)
		for(int j=k;j<=s;++j)
			if(b[i][j]-b[i-k][j]-b[i][j-k]+b[i-k][j-k]<=(k*k>>1))
				return 1;
	return 0;
}
int main(){
	s=read,k=read;
	for(int i=1;i<=s;++i)
		for(int j=1;j<=s;++j)
			a[i][j]=read,ve.push_back(a[i][j]);
	sort(ve.begin(),ve.end());
	int l=0,r=ve.size()-1,mid,ans=0;
	while(l<=r)judge(ve[mid=l+r>>1])?r=(ans=mid)-1:l=mid+1;
	cout<<ve[ans];
	return 0;
}
#include <bits/stdc++.h>
#define _overload3(_1,_2,_3,name,...)name
#define _rep(i,n)repi(i,0,n)
#define repi(i,a,b)for(int i=int(a),i##_len=(b);i<i##_len;++i)
#define MSVC_UNKO(x)x
#define rep(...)MSVC_UNKO(_overload3(__VA_ARGS__,repi,_rep,_rep)(__VA_ARGS__))
#define all(c)c.begin(),c.end()
#define write(x)cout<<(x)<<'\n'
using namespace std; using ll = long long; template<class T>using vv = vector<vector<T>>;
template<class T>auto vvec(int n, int m, T v) { return vv<T>(n, vector<T>(m, v)); }
constexpr int INF = 1 << 29, MOD = int(1e9) + 7; constexpr ll LINF = 1LL << 60;
struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); }; }aaaa;

int main() {
    int N, K;
    cin >> N >> K;
    vv<int> A = vvec(N, N, 0);
    rep(i, N) rep(j, N) cin >> A[i][j];

    int low = -1, high = 1e9;
    vv<int> B = vvec(N + 1, N + 1, 0);
    while (high - low > 1) {
        int mid = (low + high) / 2;
        rep(i, N) rep(j, N) {
            B[i + 1][j + 1] = B[i + 1][j] + B[i][j + 1] - B[i][j] + (A[i][j] > mid);
        }
        bool ok = false;
        int border = K * K / 2 + 1;
        rep(i, 0, N - K + 1) rep(j, 0, N - K + 1) {
            int c = B[i + K][j + K] - B[i + K][j] - B[i][j + K] + B[i][j];
            if (c < border) ok = true;
        }
        if (ok) high = mid;
        else low = mid;
    }
    write(high);
}