#include<bits/stdc++.h>
using namespace std;
int a[801][801],b[640001],n,k,sum[801][801];
bool check(int x){
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++){
			if(a[i][j]>x) sum[i][j]=sum[i][j-1]+sum[i-1][j]-sum[i-1][j-1]+1;
			else sum[i][j]=sum[i][j-1]+sum[i-1][j]-sum[i-1][j-1];
		}
		for(int i=1;i<=n-k+1;i++)
			for(int j=1;j<=n-k+1;j++){
				if(sum[i-1][j-1]+sum[i+k-1][j+k-1]-sum[i+k-1][j-1]-sum[i-1][j+k-1]<k*k/2+1)
					return true;
			}
		return false;
}
int main(){
	cin>>n>>k;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>a[i][j];
			b[(i-1)*n+j]=a[i][j];
		}
	}
	sort(b+1,b+n*n);
	int l=0,r=n*n;
	while(l<=r){
		int mid=(l+r)/2;
		if(check(b[mid])) r=mid-1;
		else l=mid+1;
	}
	cout<<b[r+1]<<endl;
	return 0;
}
/*** author: yuji9511 ***/
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
using vll = vector<ll>;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i=(m);i<(n);i++)
#define rrep(i,m,n) for(ll i=(m);i>=(n);i--)
ostream& operator<<(ostream& os, lpair& h){ os << "(" << h.first << ", " << h.second << ")"; return os;}
#define printa(x,n) for(ll i=0;i<n;i++){cout<<(x[i])<<" \n"[i==n-1];};
void print() {}
template <class H,class... T>
void print(H&& h, T&&... t){cout<<h<<" \n"[sizeof...(t)==0];print(forward<T>(t)...);}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

void solve(){
    ll N,K;
    cin >> N >> K;
    vector<vll> A(N, vll(N));
    rep(i,0,N){
        rep(j,0,N){
            cin >> A[i][j];
        }
    }
    ll lv = -1, rv = INF;
    while(rv - lv > 1){
        ll mid = (lv + rv) / 2;
        vector<vll> sum(N+1, vll(N+1, 0));
        rep(i,0,N){
            rep(j,0,N){
                if(A[i][j] <= mid) sum[i+1][j+1]++;
            }
        }
        rep(i,0,N+1){
            rep(j,0,N){
                sum[i][j+1] += sum[i][j];
            }
        }
        rep(j,0,N+1){
            rep(i,0,N){
                sum[i+1][j] += sum[i][j];
            }
        }
        bool ok = false;
        rep(i,0,N-K+1){
            rep(j,0,N-K+1){
                if(sum[i+K][j+K] - sum[i+K][j] - sum[i][j+K] + sum[i][j] >= (K*K+1)/2) ok = true;
            }
        }
        if(ok){
            rv = mid;
        }else{
            lv = mid;
        }
    }
    ll ans = rv;
    print(ans);



}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    solve();
}