#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define inf 1e9
#define linf 1e18
#define BASE 1000000
#define EPS 1e-10
#define PI acos(-1)
#define pii pair<int,int>
#define piii pair<int, pair<int,int>>
#define pli pair<long long, int>
#define pll pair<long long, long long>
#define pdi pair<double,int>
#define vi vector<int>
#define fi first
#define se second
#define ALL(x) (x).begin(), (x).end()
#define ms(x,val_add) memset(x, val_add, sizeof(x))
#define pb(x) push_back(x)
#define make_unique(x) sort(ALL(x)) ; x.erase( unique(ALL(x)), x.end()) ;
#define dbg(x) do { cout << #x << " = " << x << endl; } while(0)
#define mp(x, y) make_pair(x, y)
template<class T> bool ckmin(T& a, const T& b) { 
	return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { 
	return a < b ? a = b, 1 : 0; } 

bool exitInput = false;
int ntest = 1, itest = 1 ;

const int dx[4] =
{
	-1, 0, 1, 0
};
const int dy[4] =
{
	0, 1, 0, -1
};

void addMod( ll &x, ll y, ll Mod )
{
	x = ( x + y % Mod ) % Mod;
}

ll modPow( ll x, ll p, ll Mod )
{
	if( p == 0 )
		return 1;
	ll r = modPow( x, p / 2, Mod );
	r = ( r * r ) % Mod;
	if( p & 1 )
		r = ( r * x ) % Mod;
	return r;
}

// copy BigInt from Um_nik
/* using Int = __int128;
Int gcd( Int x, Int y )
{
	return y == 0 ? x : gcd( y, x % y );
}
void printInt( Int x )
{
	if ( x == 0 )
	{
		cout << 0; //printf("0");
		return;
	}
	vector<int> w;
	while( x > 0 )
	{
		w.push_back( x % 10 );
		x /= 10;
	}
	reverse( ALL( w ) );
	for ( int z : w )
		cout << z; //printf("%d", z);
}
void printAns(Int p, Int q) {
	Int g = gcd(p, q);
	p /= g;
	q /= g;
	printInt(p);
	cout << "/";
	printInt(q);
	cout << endl;
} */

ll gcd(ll p, ll q)
{
	return __gcd(p, q);
}

/*** CODE START HERE ***/
const ll Mod = 1000000007LL;
const int maxn = 800 + 5;

int n, K, a[maxn][maxn];
int b[maxn][maxn], sum[maxn][maxn];

int getsum(int i, int j, int r, int c)
{
    return sum[r][c] - sum[r][j-1] - sum[i-1][c] + sum[i-1][j-1];
}

bool isok(int h)
{
    static const int need = K*K / 2 + (K & 1);
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= n; ++j)
        {
            b[i][j] = a[i][j] <= h;
            sum[i][j] = sum[i-1][j] + sum[i][j-1] + b[i][j] - sum[i-1][j-1];
        }
    }
    for(int i = 1; i + K - 1 <= n; ++i)
    {
        for(int j = 1; j + K - 1 <= n; ++j)
        {
            int r = i + K - 1, c = j + K - 1;
            if( getsum(i, j, r, c) >= need )
            {
                return true;
            }
        }
    }
    return false;
}

int main()
{
#ifdef HOME
	freopen( "input.txt", "r", stdin );
	//freopen( "output.txt", "w", stdout );
#endif // HOME

	ios::sync_with_stdio( false );
	cin.tie( 0 );
	cout.tie( NULL );
	cout << setprecision( 10 );
	cout << fixed;
	
	//cin >> ntest;
 
	for( itest = 1; itest <= ntest; ++itest )	
	{		
        cin >> n >> K;
        
        int mi = inf, mx = 0;
        for(int i = 1; i <= n; ++i)
        {
            for(int j = 1; j <= n; ++j)
            {
                cin >> a[i][j];
                ckmin(mi, a[i][j]);
                ckmax(mx, a[i][j]);
            }
        }
        int lo = mi, hi = mx, mid, res = -1;
        while(lo <= hi)
        {
            mid = (lo + hi)>>1;
            if(isok(mid))
            {
                res = mid;
                hi = mid - 1;
            }
            else lo = mid + 1;
        }
        cout << res << endl;
	}
	
	#ifdef HOME
	cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
	#endif

	return 0;
}
#include<iostream>
#include<vector>
#include<math.h>
#include<algorithm>
#include<stack>
#include<list>
#include<queue>
#include<set>
#include<string>
#include<string.h>
#include <sstream>
#include<map>
#include<iomanip>

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization("unroll-loops")

/*Ordered set*/
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

#define ll long long
#define PI 3.14159265
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
using namespace std;

const ll MOD = 1000000007;

ll gcd(ll a,ll b){
    if(a==0)
        return b;
    return gcd(b%a,a);
}

ll lcm(ll a,ll b){
    return (a*b)/gcd(a,b);
}

bool isPrime(ll n){
	for(ll i = 2; i <=sqrt(n); i++)
		if(n % i == 0)
			return 0;
	return 1;
}

string binaryRepresentation(int x){
    string s="";
    for (int i = 31; i >= 0; i--)
    {
        if (x&(1<<i)) cout << "1";
        else cout << "0";
    }
    return s;
}

ll combination(ll n,ll r){
    if(r==n || r==0)
        return 1LL;
    return combination(n-1,r-1) + combination(n-1,r);
}

bool isPowerOfTwo(ll n){
   if(n==0)
        return false;
   return (ceil(log2(n)) == floor(log2(n)));
}

ll power(ll a,ll b){
	ll ans=1;
	while(b)
	{
		if(b&1)
			ans=(ans*a)%MOD;
		b/=2;
		a=(a*a)%MOD;
	}
	return ans%MOD;
}

ll inverse(ll i){
    if(i==1) return 1;
    return (MOD-((MOD/i)*inverse(MOD%i))%MOD+MOD)%MOD;
}

string toString(ll a){
    ostringstream temp;
    temp << a;
    return temp.str();
}

ll toNumber(string s){
    stringstream num(s);
    ll x=0;
    num>>x;
    return x;
}

int main()
{
    //vector<vector<ll> >ar(n,vector<ll> (m,0));
    //vector<ll>::iterator ptr;

    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    ll n,k;
    cin>>n>>k;
    vector<vector<ll> >ar(n,vector<ll>(n,0));
    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<n;j++)
        {
            ll x;
            cin>>x;
            ar[i][j]=x;
        }
    }

    ll low=0;
    ll high=1e9+1;
    ll med=(k*k)/2+1;
    ll r=k-1;
    while(low<=high)
    {
        ll mid=(low+high)/2;
        //cout<<" mid : "<<mid<<"\n";
        /* Mid will be the possible median value */
        vector<vector<ll> >temp=ar;
        for(ll i=0;i<n;i++)
        {
            for(ll j=0;j<n;j++)
            {
                if(ar[i][j]>mid)
                    temp[i][j]=1;
                else
                    temp[i][j]=0;
            }
        }

        //for(ll i=0;i<n;i++)
        //{
        //    for(ll j=0;j<n;j++)
        //        cout<<temp[i][j]<<" ";
         //   cout<<"\n";
        //}

        for(ll i=0;i<n;i++)
        {
            for(ll j=1;j<n;j++)
            {
                temp[i][j]+=temp[i][j-1];
            }
        }

        for(ll i=1;i<n;i++)
        {
            for(ll j=0;j<n;j++)
            {
                temp[i][j]+=temp[i-1][j];
            }
        }
        bool flag=false;
        for(ll i=r;i<n;i++)
        {
            for(ll j=r;j<n;j++)
            {
                ll sum=temp[i][j];
                ll row1=i-r;
                ll col1=j-r;
                if(row1>0)
                    sum-=temp[row1-1][j];
                if(col1>0)
                    sum-=temp[i][col1-1];
                if(row1>0 && col1>0)
                    sum+=temp[row1-1][col1-1];
                //cout<<" i : "<<i<<" j : "<<j<<" sum : "<<sum<<"\n";
                if(sum<med)
                {
                    flag=true;
                }
            }
        }

        if(flag)
            high=mid-1;
        else
            low=mid+1;

    }
    cout<<low;

    return 0;
}
