#include<bits/stdc++.h>
using namespace std;
using namespace std::chrono;         
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long 
#define rep(i,a,b)    for(int i=a;i<b;i++)
#define repn(i,a,b)   for(int i=a;i>=b;i--)
#define F first
#define S second
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define mod 1000000007  

int fexp(int a, int b,int m){int ans = 1;while(b){if(b&1)ans=(ans*a)%m; b/=2;a=(a*a)%m;}return ans;}
int inverse(int a, int p){return fexp(a, p-2,p);} 

// int fac[2000001];

// int nCrModPFermat(int n, int r,int p) { 
//     // cout<<1<<endl;
//     if (r==0) return 1; 
//     return (((fac[n]*((((inverse(fac[r],p)%p)*inverse(fac[n-r],p))%p))))%p); 
// }

// vi spf(10000001,1);
vector<vector<int>> v(1000,vector<int>(1000,0));
int k,n;
bool check(int num){
    vector<vector<int>> dp(n+1,vector<int>(n+1,0));
    int val=(k*k+1);val=val/2;
    rep(i,0,n){
        rep(j,0,n){
            if(v[i][j]>num)dp[i][j]=0;
            else dp[i][j]=1;
            if(j-1>=0)dp[i][j]+=dp[i][j-1];
            if(i-1>=0)dp[i][j]+=dp[i-1][j];
            if(i&&j)dp[i][j]-=dp[i-1][j-1];
        }
        
    }
    int mx=0;
    rep(i,0,n-k+1){
        rep(j,0,n-k+1){
            int ans=dp[i+k-1][j+k-1];
            if(i){
                ans-=dp[i-1][j+k-1];
                if(j)ans+=dp[i-1][j-1];
            }
            if(j)ans-=dp[i+k-1][j-1];
            // cout<<ans<<endl;
            mx=max(mx,ans);
        }
    }
    // cout<<num<<"__"<<mx<<endl;
    return (mx>=val);
    // return true;
}


void solve(){
    cin>>n>>k;
    rep(i,0,n){
        rep(j,0,n){
            cin>>v[i][j];
        }
    }
    int l=0,r=mod,ans=mod;
    while(l<r){
        int mid=(l+r)/2;
        if(check(mid))r=mid;
        else l=mid+1;
    }
    cout<<r;


}
         
signed main(){
        IOS;    
    
        auto start = high_resolution_clock::now();
        int t=1; 
        // cin>>t;
        // spf[1]=1;
        // for(int i=2; i<=10000001; i++){
        // if(spf[i]==1){
        //     // allprime.pb(i);
        //     for(int j=i; j<=10000001; j+=i)
        //         if(spf[j]==1)spf[j]=i;
        // }
        // }
        // fac[0] = 1; 
        // for (int i=1 ; i<=200001; i++) fac[i] = fac[i-1]*i%mod; 

        rep(i,0,t){
            // cout<<"Case #"<<i+1<<": ";
            solve();
            cout<<"\n";
        }
        auto stop = high_resolution_clock::now();
        auto duration = duration_cast<microseconds>(stop - start);
        cerr<<"Time taken:"<<duration.count()/1000000.0<<"seconds"<<"\n";
    //    return 0;
}
#include<bits/stdc++.h>
using namespace std;

int main(){
    int n, k, i, j, cent; cin >> n >> k;
    cent = (k * k) - int(k * k / 2);
    int a[800][800], s[801][801];
    
    for(i = 0; i < n; i++){
        for(j = 0; j < n; j++) cin >> a[i][j];
    }
    for(i = 0; i <= n; i++) s[i][0] = 0, s[0][i] = 0;

    int L = 0, R = 1000000000, mid;
    while(L != R){
        mid = (L + R) / 2;

        for(i = 1; i <= n; i++){
            for(j = 1; j <= n; j++){
                s[i][j] = s[i][j-1] + s[i-1][j] - s[i-1][j-1];
                if(a[i-1][j-1] <= mid){
                    s[i][j]++;
                }
            }
        }
        
        bool exist = false;
        for(i = 1; i <= n - (k - 1) && !exist; i++){
            for(j = 1; j <= n - (k - 1) && !exist; j++){
                if(s[i+(k-1)][j+(k-1)] - s[i-1][j+(k-1)] - s[i+(k-1)][j-1] + s[i-1][j-1] >= cent){
                    exist = true;
                }
            }
        }
        exist ? R = mid : L = mid + 1;
    }
    
    cout << L << endl;

    return 0;
}