#include <bits/stdc++.h>

typedef long double ld;
#define int long long

#define gcd            __gcd
#define endl           "\n"
#define setbits(x)     __builtin_popcountll(x)
#define zrobits(x)     __builtin_ctzll(x)
#define mod            1000000007
#define mod2           998244353
#define maxe           *max_element
#define mine           *min_element
#define inf            1e18
#define pb             push_back
#define all(x)         x.begin(), x.end()
#define f              first
#define s              second
#define lb             lower_bound
#define ub             upper_bound
#define ins            insert
#define sz(x)          (int)(x).size()
#define mk             make_pair
#define deci(x, y)     fixed<<setprecision(y)<<x
#define w(t)           int t; cin>>t; while(t--)
#define nitin          ios_base::sync_with_stdio(false); cin.tie(nullptr)
#define PI             3.141592653589793238
#define mem0(x)        memset(x,0,sizeof x)
#define mem1(x)        memset(x,-1,sizeof x)
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.f << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }

#ifdef NITIN
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

int a[801][801];
int dp[801][801];
int n,k;
bool good(int val) {
    int req = k*k-((k * k) / 2+1)+1;
    mem0(dp);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            dp[i][j] = (a[i][j] <= val);
            if (i >= 1 || j >= 1) {
                if (i >= 1) dp[i][j] += dp[i - 1][j];
                if (j >= 1) dp[i][j] += dp[i][j - 1];
                if(i>=1 && j>=1) dp[i][j]-=dp[i-1][j-1];
            }
        }
    }
    for (int i = 0; i <= n - k; i++) {
        for (int j = 0; j <= n - k; j++) {
            int tot = dp[i + k - 1][j + k - 1];
            if (i >= 1) {
                tot -= dp[i - 1][j + k - 1];
            }
            if (j >= 1) {
                tot -= dp[i + k - 1][j - 1];
            }
            if (i >= 1 && j >= 1) {
                tot += dp[i - 1][j - 1];
            }
            if (tot>=req) {
                return true;
            }
        }
    }
    return false;
}
void solve() {
    cin>>n>>k;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cin>>a[i][j];
        }
    }
    int l=-1,r=1e9+1;
    while(l+1<r) {
        int mid = (l + r) / 2;
        if (good(mid)) {
            r = mid;
        } else {
            l = mid;
        }
    }
    cout<<r<<endl;
}

int32_t main() {
    nitin;

    solve();
}
#include<bits/stdc++.h>
#define LL long long
#define DB double
#define pb push_back
#define mkp make_pair
#define LB lower_bound
#define UB upper_bound
#define rint register int
#define MS(x,y) memset(x,y,sizeof(x))
#define rep(i,a,b) for(rint i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(rint i=a,i##end=b;i>=i##end;--i)
#define cms printf("%.2lf\n",(&o2-&o1)/1024.0/1024)
using namespace std;bool o1;
char IO;
inline int Rd(){
	int _s_=0;
	bool _f_=0;
	while(!isdigit(IO=getchar()))
		IO=='-'&&(_f_=1);
	do _s_=(_s_<<1)+(_s_<<3)+(IO^48);
	while(isdigit(IO=getchar()));
	return _f_?-_s_:_s_;
}
const int M=803;
int n,k,t,a[M][M],b[M*M],tt,ans,l,r,c[M][M],d[M][M];
int ask(int x,int y){
	return d[x][y]-d[x-k][y]-d[x][y-k]+d[x-k][y-k];
}
bool chk(int x){
	rep(i,1,n)
		rep(j,1,n)
			if(a[i][j]<=x)
				c[i][j]=1;
			else c[i][j]=0;
	rep(i,1,n)
		rep(j,1,n)
			d[i][j]=d[i-1][j]+d[i][j-1]+c[i][j]-d[i-1][j-1];
	rep(i,k,n){
		rep(j,k,n){
			if(ask(i,j)>=tt)
				return 1;
		}
	}
	return 0;
}
bool o2;int main(){
	n=Rd(),k=Rd(),tt=k*k-((k*k)/2+1)+1;
	rep(i,1,n)
		rep(j,1,n)
			b[++t]=a[i][j]=Rd();
	sort(b+1,b+1+t);
	t=unique(b+1,b+1+t)-b-1;
	rep(i,1,n){
		rep(j,1,n){
			a[i][j]=LB(b+1,b+1+t,a[i][j])-b;
		}
	}
	chk(4);
	ans=t;
	l=1,r=t;
	while(l<=r){
		int mid=l+r>>1;
		if(chk(mid)){
			ans=mid;
			r=mid-1;
		}
		else l=mid+1;
	}
	cout<<b[ans];
	return 0;
}