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

#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define FOR(i, a, b) for (int i = a; i <= (b); i++)
#define ROF(i, a, b) for (int i = a; i >= (b); i--)
#define y1 awefakhlrv

using pii = pair<int, int>; using vpii = vector<pii>;
using vi = vector<int>; using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>; using vpll = vector<pll>;
using vll = vector<ll>; using vvll = vector<vll>;

int main() {
    int n, k; cin >> n >> k;
    vvi a(n + 1, vi(n + 1));
    FOR(i, 1, n) FOR(j, 1, n)
        cin >> a[i][j];
    int ans = 0x3f3f3f3f, nd = k * k / 2 + 1;
    ROF(bl, 29, 0) {
        int na = ans - (1 << bl);
        if (na < 0) continue;
        // cout << na << endl;
        vvi f(n + 1, vi(n + 1)), cnt(f);
        FOR(i, 1, n) FOR(j, 1, n)
            f[i][j] = (a[i][j] >= na);
        // FOR(i, 1, n) FOR(j, 1, n)
        //     cout << f[i][j] << (j == n ? '\n' : ' ');
        vvi psn(n + 1, vi(n + 1));
        FOR(i, 1, n) FOR(j, 1, n)
            psn[i][j] = psn[i][j - 1] + f[i][j];
        FOR(i, 1, n)
            FOR(j, k, n)
                cnt[i][j] = psn[i][j] - psn[i][j - k];
        vvi psm(n + 1, vi(n + 1));
        FOR(i, 1, n) FOR(j, k, n)
            psm[i][j] = psm[i - 1][j] + cnt[i][j];
        // FOR(i, 1, n) FOR(j, 1, n)
        //     cout << cnt[i][j] << (j == n ? '\n' : ' ');
        int mx = 0x3f3f3f3f;
        FOR(i, k, n) FOR(j, k, n) {
            mx = min(psm[i][j] - psm[i - k][j], mx);
        }
        // cout << endl << mx << endl;
        if (mx < nd) ans = na;
    }
    cout << ans - 1 << endl;
}
#include <bits/stdc++.h>
using namespace std;



#define ios			ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR(i,m)		for(int i = 0 ;i  < m ;i++)
#define mem(a ,b)		memset(a,  (b) , sizeof(a))
#define all(cont)		cont.begin(),cont.end()
#define mp			make_pair
#define pb			push_back
#define F			first
#define S			second
#define cn			cout<<'\n'
#define re			return 


typedef pair<int,int>		pi;
typedef	vector<int>		vi;
typedef long long 		ll;
typedef long double		ld;
typedef unsigned long long int  ull;

const int mod = 1e9 +7;


int a[801][801];
int p[801][801];
int n  , k;


bool check(int x){
	memset(p , 0 , sizeof(p));

	for(int i = 1 ;i <= n ;i++){
		for(int j = 1 ;j <= n ;j++){
			p[i][j] = (a[i][j] <= x ? 1 : 0);
		}
	}

	for(int i =1  ;i <= n;i++){
		for(int j = 1; j <= n ;j++){
			p[i][j] += p[i-1][j] + p[i][j-1] - p[i-1][j-1];
		}
	}

	int max_val = 0;
	for(int i = k; i <= n ;i++){
		for(int j = k ;j <= n ;j++){
			max_val = max(max_val , p[i][j] - p[i-k][j] - p[i][j-k] + p[i-k][j-k]);
		}
	}
	return max_val >= (k*k + 1)/2;
}






void solve(){

	cin >> n >> k;

	for(int i = 1 ;i <= n ;i++){
		for(int j = 1 ; j <= n ;j++)cin>>a[i][j];
	}


	int l = 0 , r = mod-7;

	while(l < r){
		int mid = l + (r-l)/2;
		
		if(check(mid)){
			r = mid;
		}
		else l = mid + 1;
	}
	cout<<l<<"\n";


}





int32_t main(){

	ios;



	int _t = 1;
//	cin >> _t;

	for(int i = 1 ; i  <= _t ;i++){
//		cout<< "Case #"<<i<<": ";
		solve();
	}


	return 0;
}
