#include <bits/stdc++.h>
//#include <atcoder/all>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

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

const long long INF = 1e18;
//const ll mod = 1000000007;
ll N, K;
ll A[1000][1000];
ll B[1000][1000];

bool f(ll border) {
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++) {
            B[i+1][j+1] = (A[i][j] <= border);
        }
    }
    for(int i = 1; i <= N; i++) {
        for(int j = 1; j <= N; j++) {
            B[i][j] = B[i][j] + B[i-1][j] + B[i][j-1] - B[i-1][j-1];
        }
    }
    for(int i = 0; i + K <= N; i++) {
        for(int j = 0; j + K <= N; j++) {
            ll num = B[i+K][j+K] - B[i][j+K] - B[i+K][j] + B[i][j];
            if(num >= (K * K + 1) / 2) return true;
        }
    }
    return false;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N >> K;
    for(int i = 0; i < N; i++) {
        for(int j = 0; j < N; j++) cin >> A[i][j];
    }
    ll ok = 1e10;
    ll ng = -1;
    while(abs(ok - ng) > 1) {
        ll mid = (ok + ng) / 2;
        if(f(mid)) ok = mid;
        else ng = mid;
    }
    cout << ok << endl;
    return 0;
}

#include<bits/stdc++.h> 
using namespace std;
namespace Ruri{
    #define ms(a,b) memset(a,b,sizeof(a))
    #define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ?
    #define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i)
    #define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
    #define ce(i,r) i==r?'\n':' '
    #define pb push_back 
    #define all(x) x.begin(),x.end()
    #define gmn(a,b) a=min(a,b)
    #define gmx(a,b) a=max(a,b)
    #define fi first
    #define se second                         
    typedef long long ll;
    typedef unsigned long long ull;
    typedef double db;
    const int infi=1e9;//infi较大，注意涉及inf相加时爆int
    const ll infl=4e18;
    inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; }
    inline ll pos_mod(ll a,ll b){ return (a%b+b)%b; }
    //std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0));
}
using namespace Ruri;
namespace Read{
    #define ss(a) scanf("%s",a)
    inline int ri(){ int x; scanf("%d",&x); return x; }
    inline ll rl(){ ll x;  scanf("%lld",&x); return x; }
    inline db rd(){ db x; scanf("%lf",&x); return x; }
}
namespace DeBug{
    #define pr(x) cout<<#x<<": "<<(x)<<endl
    #define pra(x,a,b) cout<<#x<<": "<<endl; \
            repi(i,a,b) cout<<x[i]<<" ";     \
            puts("");   
    #define FR(a) freopen(a,"r",stdin)
    #define FO(a) freopen(a,"w",stdout)
}
using namespace Read;
using namespace DeBug;
const int MAX_N=805;
int n,k,median;
int a[MAX_N][MAX_N];
int sum[MAX_N][MAX_N];
bool Check(int x)
{
    repi(i,1,n)repi(j,1,n) sum[i][j]=(a[i][j]>x);
    repi(i,1,n)repi(j,1,n) sum[i][j]+=(sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]);
    repi(i,k,n)repi(j,k,n)if((sum[i][j]-sum[i-k][j]-sum[i][j-k]+sum[i-k][j-k])<median) return true;
    return false;
}
int main()
{
    n=ri(),k=ri();
    median=(k*k/2)+1;
    repi(i,1,n)repi(j,1,n) a[i][j]=ri();
    int l=0,r=1000000000,ans;
    while(l<=r){
        int mid=(l+r)>>1;
        if(Check(mid)) ans=mid,r=mid-1;
        else l=mid+1;
    }
    printf("%d\n",ans);


    return 0;
}