#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fo(i,a,b) for(int i = a; i<b ; i++)
#define FIO ios_base::sync_with_stdio(false);   cin.tie(NULL);   cout.tie(NULL);
#define pb push_back
#define M 1000000007
int mod(int x){
return ((x%M + M)%M);
}
int add(int a,int b){
return mod(mod(a)+mod(b));
}
int mul(int a,int b){
return mod(mod(a)*mod(b));
}
// ****************************************************************************
int mat[801][801];
int n,k;
bool isValid(int mid){
    int dp[n+1][n+1];

    memset(dp,0,sizeof dp);
    fo(i,1,n+1){
        fo(j,1,n+1){
            if(mat[i][j]>mid)
            dp[i][j]=1;
        }
    }
    fo(i,1,n+1){
        fo(j,1,n+1)
        dp[i][j]+=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1];
    }
   
    int pos = ((k*k)/2);
    fo(i,k,n+1){
        fo(j,k,n+1){
            int temp = dp[i][j]-dp[i-k][j]-dp[i][j-k]+dp[i-k][j-k];
            if(temp<=pos)
            return true;
        }
    }
    return false;
}
void solve(){
    cin>>n>>k;
    fo(i,1,n+1)
    fo(j,1,n+1)
    cin>>mat[i][j];
    int s = 0,e=INT_MAX;
    int ans = e;
    while(s<=e){
        int mid = s + (e-s)/2;
        if(isValid(mid)){
            ans=mid;
            e=mid-1;
        }
        else s=mid+1;
    }
    cout<<ans<<"\n";
}
signed main()
{
FIO
int t;
t=1;
// cin>>t;
while(t--)
{
solve();
}
}
#include<bits/stdc++.h>
using namespace std;
const int N=800+5;
int n,k;
int a[N][N];
struct point{
	int x,y;
	int a;
	bool operator <(const point &p)const{
		return a<p.a;
	}
}s[N*N];
int cnt;
int f[N][N];
int get(int l,int r,int x,int y){
	return f[x][y]-f[x][r-1]-f[l-1][y]+f[l-1][r-1];
}
bool check(int mid){
	for(int i=1;i<=n;++i){
		for(int j=1;j<=n;++j){
			if(a[i][j]<=s[mid].a)f[i][j]=1;
			else f[i][j]=0;
		}
	}
	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j)
			f[i][j]+=f[i][j-1]+f[i-1][j]-f[i-1][j-1];
		
	for(int i=k;i<=n;++i)
		for(int j=k;j<=n;++j)
			if(get(i-k+1,j-k+1,i,j)>=k*k-k*k/2)return 1;
	return 0;
}
int main(){
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;++i)
		for(int j=1;j<=n;++j){
			scanf("%d",&a[i][j]);
			s[++cnt]=(point){i,j,a[i][j]};
		}
	sort(s+1,s+cnt+1);	
	int l=1,r=n*n,ans;
	while(l<=r){
		int mid=l+r>>1;
		//printf("mid=%d\n",mid);
		if(check(mid))r=mid-1,ans=mid;
		else l=mid+1;
	}
	printf("%d\n",s[ans].a);
	return 0;
}