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

typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
const int INF = int (1e9) + int (1e5);
const ll INFL = ll(2e18) + ll(1e10);
const ui MOD = 1E9 + 7;
const double EPS = 1e-9;
#define FOR(i,n) for (int i=0;i<(n);++i)
#define ROF(i,x) for(int i = (x) ; i >= 0 ; --i)
#define MP make_pair
#define all(a) (a).begin(),(a).end()
#define ODD(x) ( ((x)&1)==0?0:1 )
#define SIGN(x) ( ((x) > 0) - ((x) < 0)  )
#define dbg(...) cerr << #__VA_ARGS__ << ": " << (__VA_ARGS__) << endl
std::mt19937_64  generator(std::chrono::system_clock::now().time_since_epoch().count());

inline ll powmod(ll a,ll b,ll mod) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll lcm(ll a, ll b) { return a / __gcd(a,b) * b; }


void READ(bool _local){
    ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef _DEBUG
    if (_local)
        freopen ("in.txt", "r", stdin);
#endif
}

#define SI() (cout << "Yes\n")
#define NO() (cout << "No\n")

void solve(){

}

int main() {
    READ(0);
    int n,k;cin>>n>>k;
    vvi a(n,vi(n));
    vi b(n*n);
    FOR(i,n){
        FOR(j,n) {
            cin>>a[i][j];
            b[i*n+j]=a[i][j];
        }
    }
    sort(all(b));
    int lo=0,hi=n*n-1;
    const int A=(k*k+1)/2;
    while(lo<hi){
        int mid=(lo+hi)/2;
        vvi x(n,vi(n));
        FOR(i,n)FOR(j,n) x[i][j]=a[i][j]<=b[mid];
        for(int j=1;j<n;++j) x[0][j] += x[0][j-1];
        for(int i=1;i<n;++i) x[i][0] += x[i-1][0];
        for(int i=1;i<n;++i){
            for(int j=1;j<n;++j){
                x[i][j] += x[i-1][j] + x[i][j-1] - x[i-1][j-1];
            }
        }
        int cnt=-1;
        for(int i=k-1;i<n && cnt<A;++i){
            for(int j=k-1;j<n;++j){
                cnt = x[i][j];
                if (i-k>=0) cnt -= x[i-k][j];
                if (j-k>=0) cnt -= x[i][j-k];
                if (i-k>=0 && j-k>=0) cnt += x[i-k][j-k];
                if (cnt>=A) break;
            }
        }
        if (cnt>=A) hi=mid;
        else lo=mid+1;
    }
    cout << b[lo];

    return 0;
}


#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n, k;
	cin >> n >> k;
	
	vector <vector <int> > a(n, vector <int> (n));
	
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			cin >> a[i][j];
			
	int left = -1, right = 1000000000, mid;
	
	while (left < right - 1)
	{
		mid = (left + right) / 2;
		
		vector <vector <int> > b(n, vector <int> (n));
		
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				b[i][j] = a[i][j] <= mid;
				
		vector <vector <int> > pref_sum(n, vector <int> (n));
		
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				if (i == 0 && j == 0)
					pref_sum[0][0] = b[0][0];
				else if (i == 0)
					pref_sum[i][j] = pref_sum[i][j - 1] + b[i][j];
				else if (j == 0)
					pref_sum[i][j] = pref_sum[i - 1][j] + b[i][j];
				else
					pref_sum[i][j] = pref_sum[i - 1][j] + pref_sum[i][j - 1] - pref_sum[i - 1][j - 1] + b[i][j];
	
		int flag = 0;
	
		for (int i = 0; i + k <= n; i++)
			for (int j = 0; j + k <= n; j++)
			{
				int cur_sum;
				
				if (i == 0 && j == 0)
					cur_sum = pref_sum[k - 1][k - 1];
				else if (i == 0)
					cur_sum = pref_sum[k - 1][j + k - 1] - pref_sum[k - 1][j - 1];
				else if (j == 0)
					cur_sum = pref_sum[i + k - 1][k - 1] - pref_sum[i - 1][k - 1];
				else
					cur_sum = pref_sum[i + k - 1][j + k - 1] - pref_sum[i - 1][j + k - 1] - pref_sum[i + k - 1][j - 1] + pref_sum[i - 1][j - 1];
					
				if (cur_sum >= (k * k + 1) / 2)
					flag = 1;
			}
			
		if (flag)
			right = mid;
		else
			left = mid;
	}
	
	cout << right;
}