#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int,int>
#define vpii vector<pair<int,int>>
#define pqueue priority_queue
#define umap unordered_map
#define uset unordered_set
#define bit(s, i) ((1 << i) & s ? 1 : 0)
#define bits(x) __builtin_popcount((x))
#define gcd(x, y) __gcd(x, y)
#define lcm(x, y) ((x) / gcd(x, y) * (y))
#define inf 4557430888798830399LL
#define mst(dp, val) memset(dp, val, sizeof(dp))
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define repd(i, a, b, d) for (int i = a; i <= b; i += d)
#define rep_(i, a, b) for (int i = a; i >= b; --i)
#define repd_(i, a, b) for (int i = a; i >= b; i -= d)
#define mxm(a, b) a = max(a, b) 
#define mnm(a, b) a = min(a, b)
#define pb push_back
#define X first
#define Y second
#define L size()
#define all(x) (x).begin(), (x).end()
#define debug(x) cout << #x << "=" << x << endl
#define readArr(a, n) for (int i = 1; i <= n; i++) cin >> a[i];
#define printArr(a, n) for (int i = 1; i <= n; i++) cout << a[i] << " "; cout << endl;
#define print(e) cout << (e) << endl
const int mod = 1e9 + 7;
#define Add(x, val) x = (x + (val)) % mod
// #define MULTI_CASES
// #define PRE

const int N = 1e5 + 5;

int mat[805][805];
int sum[805][805]; 

void solve() {
    int n, k;
    cin >> n >> k;
    rep(i, 1, n)
    rep(j, 1, n) cin >> mat[i][j];

    int lo = 0, hi = 1e9;
    while (lo < hi) {
        int mi = (lo + hi) / 2;
        rep(i, 1, n)
        rep(j, 1, n) {
            int e = mat[i][j] <= mi;
            sum[i][j] = e + sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1];
        }
        bool flag = false;
        rep(i, k, n)
        rep(j, k, n) {
            int cnt = sum[i][j] - sum[i][j-k] - sum[i-k][j] + sum[i-k][j-k];
            if (cnt >= (k*k+1)/2) { flag = true; break; }
        }
        if (flag) hi = mi;
        else lo = mi + 1;
    }

    print(lo);

}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    #ifdef PRE
        pre();
    #endif
    #ifdef MULTI_CASES
        int t; cin >> t;
        while (t--) solve();
    #else
        solve();
    #endif
    return 0;
}
#include<set>
#include<queue>
#include<bitset>
#include<cmath>
#include<ctime>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAXN 805
#define ENDL putchar('\n')
#define LL long long
#define DB double
#define lowbit(x) ((-x) & (x))
#define INF 0x3f3f3f3f
#define BT bitset<200002>
LL read() {
	LL f = 1,x = 0;char s = getchar();
	while(s < '0' || s > '9') {if(s=='-')f = -f;s = getchar();}
	while(s >= '0' && s <= '9') {x=x*10+(s-'0');s = getchar();}
	return f * x;
}
int n,m,i,j,s,o,k;
int a[MAXN][MAXN];
int sm[MAXN][MAXN];
bool check(int md) {
	int rq = k*k/2+(k*k & 1);
	for(int i = 1;i <= n;i ++) {
		for(int j = 1;j <= n;j ++) {
			sm[i][j] = (a[i][j] <= md ? 1:0);
			sm[i][j] += sm[i][j-1];
		}
		for(int j = 1;j <= n;j ++) sm[i][j] += sm[i-1][j];
	}
	for(int i = 1;i <= n-k+1;i ++) {
		for(int j = 1;j <= n-k+1;j ++) {
			if(sm[i+k-1][j+k-1]-sm[i-1][j+k-1]-sm[i+k-1][j-1]+sm[i-1][j-1] >= rq) return 1;
		}
	}
	return 0;
}
int main() {
	n = read();k = read();
	int mx = 0;
	for(int i = 1;i <= n;i ++) {
		for(int j = 1;j <= n;j ++) {
			a[i][j] = read();
			mx = max(mx,a[i][j]);
		}
	}
	int l = 0,r = mx,mid;
	while(l < r) {
		mid = (l + r) >> 1;
		if(check(mid)) r = mid;
		else l = mid+1;
	}
	printf("%d\n",l);
	return 0;
}