#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define yes "Yes"
#define no "No"
#define rep(i, n) for( int i=0; i<(n); i++)
const long long INF = 1LL << 60;

template<class T> inline bool chmin(T& a, T b) {if(a>b){a=b; return 1;} return 0; }
template<class T> inline bool chmax(T& a, T b) {if(a<b){a=b; return 1;} return 0; }

int main(){
    int n,k;
    cin >> n >> k;
    vector<vector<int>> a(n, vector<int>(n));
    rep(i,n) rep(j,n) cin >> a[i][j];
    int L = (k*k/2)+1;

    ll wa=-1, ac = 1001001000;
    while (wa+1 < ac)
    {
        ll wj = (wa + ac)/2;
        bool ok = false;
        vector<vector<int>> s((n+1), vector<int>(n+1));
        rep(i, n) rep(j ,n) s[i+1][j+1] = a[i][j]>wj?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) ac = wj; else wa = wj;
    }
    
    cout << ac << endl;
}
#include<bits/stdc++.h>
using namespace std;
const int N=3000;
int n,k;
int a[N][N],sum[N][N];
bool check(int x)
{
	memset(sum,0,sizeof sum);
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
			
			if(a[i][j]<=x) sum[i][j]--;
			else sum[i][j]++;
		}
	}
	
	
	for(int i=k;i<=n;i++)
	{
		for(int j=k;j<=n;j++)
		{
			if(sum[i][j]-sum[i-k][j]-sum[i][j-k]+sum[i-k][j-k]<=0) return true;
		}
		
	 }
	 
	 return false;
	  
}
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]);
		}
	}
	int l =0,r=1e9;
	while(l<r)
	{
		int mid = l+r>>1;
		if(check(mid)) r=mid;
		else l=mid+1; 
	}
	
	printf("%d\n",r);
 } 