#pragma GCC optimize ("O2")
#pragma GCC target ("avx")
#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

int A[801][801];
int B[801][801];
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);


	int N, K;
	cin >> N >> K;
	rep1(i, N) rep1(j, N) cin >> A[i][j];
	int C = (K * K + 1) / 2;

	int L = -1, R = 1000000001;
	while (L + 1 < R) {
		int wj = (L + R) / 2;

		rep1(i, N) rep1(j, N) B[i][j] = (A[i][j] <= wj);
		rep1(i, N) rep1(j, N) B[i][j] += B[i][j - 1];
		rep1(j, N) rep1(i, N) B[i][j] += B[i - 1][j];

		int k = 0;
		rep(i, N - K + 1) rep(j, N - K + 1) {
			int tmp = B[i][j] - B[i][j + K] - B[i + K][j] + B[i + K][j + K];
			chmax(k, tmp);
		}
		if (k < C) L = wj;
		else R = wj;
	}

	co(R);



	Would you please return 0;
}

#include<bits/stdc++.h>
using namespace std ;


#define vt vector
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()
#define ll long long
#define ld long double
#define ar array
#define inf() ifstream cin("d.txt")
#define onf() ofstream cout("test.txt")
#define mod 1000000007
#define pb push_back
#define endl cout<<"\n";
#define prt1(a) cout<<a<<" "
#define prt2(a,b) cout<<a<<" "<<b<<" "
#define prt3(a,b,c) cout<<a<<" "<<b<<" "<<c<<" "
#define in(a) cin>>a
#define inn(a,b) cin>>a>>b
#define innn(a,b,c) cin>>a>>b>>c 
#define rep(i,st,end) for(ll i=st;i<end;i++)
#define repv(i,st,end) for(ll i=st;i>=end;i--)
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define TEST ll tc;cin>>tc;while(tc--)
#define EACH(x, a) for (auto& x: a)

template<class A> void read(vt<A>& v);
template<class A, size_t S> void read(ar<A, S>& a);
template<class T> void read(T& x) {
    cin >> x;
}
void read(double& d) {
    string t;
    read(t);
    d=stod(t);
}
void read(long double& d) {
    string t;
    read(t);
    d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
    read(h);
    read(t...);
}
template<class A> void read(vt<A>& x) {
    EACH(a, x)
        read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
    EACH(a, x)
        read(a);
}
#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a <<"\n";
    err(++it, args...);
}
const int N=820;
int k,n;
int g[N][N];
int dp[N][N];
bool check(int mid)
{
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            dp[i][j]=g[i][j]>=mid;
            
            dp[i][j]+=dp[i-1][j];
            dp[i][j]+=dp[i][j-1];
            dp[i][j]-=dp[i-1][j-1];
        }
    }
  
    ll mn =  1e9;
    for(int i=k;i<=n;i++) 
    {
        for(int j=k;j<=n;j++)
        {
            ll cnt=dp[i][j];
            cnt-=dp[i][j-k];
            cnt-=dp[i-k][j];
            cnt+=dp[i-k][j-k];
            mn = min(cnt, mn);
        }
    }
    if(mn>=k*k/2+1)
        return true;
    return false;
}
void solve(){
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            cin>>g[i][j];
    int l=0,r=1e9, ans = 0;
    while(l <= r)
    {
        int mid=(l+r)>>1;
        if(check(mid))
        {
            l= mid+1;
            ans = mid;
        } 
        else r=mid-1;
    }
    cout<<ans;
}

 int main()
 {
    clock_t start, end; 
    start = clock(); 
    fio;
    ll k = 1;
    ll tc = 1;
   // cin>>tc;
    while(tc--){
       // cout<<"Case #"<<k<<": ";k++;
        solve();
        endl;
    }
    end = clock();
     double time_taken = double(end - start) / double(CLOCKS_PER_SEC); 
    // debug(time_taken);
    
 }


