/* ** *** In the name of God *** ** */
// Only Haider is Amir al-Momenin
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define ij for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++)
const ll maxn = 810;
int a[maxn][maxn], b[maxn][maxn];
vector<int> v;
int n, k;
bool check(int x) {
	ij if (a[i][j] >= x) b[i][j] = 1; else b[i][j] = -1;
	ij b[i][j] += b[i][j-1];
	ij b[i][j] += b[i-1][j];
	for (int i = k; i <= n; i++) {
		for (int j = k; j <= n; j++) {
			int s = b[i][j] - b[i][j - k] - b[i-k][j] + b[i-k][j-k];
			if (s <= 0) return 1;
		}
	}
	return 0;
}
signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	cin >> n >> k;
	ij cin >> a[i][j], v.push_back(a[i][j]);
	sort(v.begin(), v.end());
	v.resize(unique(v.begin(), v.end()) - v.begin());
	// ij cout << a[i][j] << '\n';
	int id = k * k - (k * k / 2) + 2;
	int l = 0, r = v.size();
	while (r > l + 1) {
		int mid = (l + r) / 2;
		int V = v[mid];
		// cout << V << " ";
		if (check(V)) r = mid;
		else l = mid;
		// cout << endl;
	}
	cout << v[l];
	return 0;
}
/*
		  _ _ _ _                  _ _ _ _                _ _ _ _
		/      / \                /     / |              /     / |
	       /``````\   \              |`````|  |             |`````|  |
	      /        \   \             |     |  |             |     |  |
	     /          \   \            |     |  |             |     |  |
	    /     /\     \   \           |     |  |             |     |  |
	   /     / /\     \   \          |     |  |             |     |  |
	  /     / /  \     \   \         |     |  |             |     |  |
	 /     / /_ _ \     \   \        |     |  |             |     |  |
	/     /_ _ _ _ \     \   \       |     |  |             |     |  |
       /                      \   \      |     |  |_ _ _ _      |     |  |
      /      _ _ _ _ _ _ _     \   \     |     | /        /|    |     |  |
     /     / /            \     \   \    |     |/_ _ _ _ / |    |     |  |
    /     / /              \     \  /    |               | /    |     | /
   /_ _ _/_/                \_ _ _\/     |_ _ _ _ _ _ _ _|/     |_ _ _|/
*/
#include<iostream>
#include<climits>
#include<bits/stdc++.h>
using namespace std;
#define LL long long int
#define mod 998244353

int main() 
{
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    LL     N ,K;
    cin >> N >> K;
    vector< vector<LL> > arr(N+1,vector<LL>(N+1));
    for(LL i=0;i<N;i++){
        for(LL j=0;j<N;j++){
            cin >> arr[i][j];
        }
    }
    function<bool(LL) > checkMedianIsPossibleOrNot = [&](LL median){
        // SUBMATRIX SUM QUERIES BRO
        vector< vector<LL> > temp(N+1,vector<LL>(N+1,0));
        for(LL i=0;i<N;i++){
            for(LL j=0;j<N;j++){
                if(arr[i][j] <= median){
                    temp[i][j] = 1 ; 
                }
            }
        }
        vector< vector<LL> > dp(N+1,vector<LL>(N+1,0));
        for(LL i=1;i<=N;i++){
            for(LL j=1;j<=N;j++){
                dp[i][j]= temp[i-1][j-1] + dp[i-1][j]+ dp[i][j-1] - dp[i-1][j-1]; 
            }
        }
        for(LL i=1;i<=N-K+1;i++){
            for(LL j=1;j<=N-K+1;j++){
                LL x1=i;LL y1=j;
                LL x2=i+K-1;
                LL y2=j+K-1;
                LL suminSubmatrix = dp[x2][y2] - dp[x1-1][y2] - dp[x2][y1-1] + dp[x1-1][y1-1];
                if(suminSubmatrix >= (K*K+1)/2){
                    return true;
                }  
            }
        }
        return false;
    };
    LL start =      -1;
    LL end   = (LL)1e9;
    LL ans = -1;
    while(start <= end){
        LL mid = (start+end)/2;
        if(checkMedianIsPossibleOrNot(mid)){
            end=mid-1;
            ans=mid;
        }
        else{
            start=mid+1;
        }
    }
    cout<<ans;

}

