/*
求 k*k 矩阵最小中位数值
*/
#include <bits/stdc++.h>
using namespace std;
const int maxx=805;
long long mp[maxx][maxx];
long long sum[maxx][maxx];
int n,k,inx;
long long l=1e9+5,r=-1;
int check(int x){//每个小矩阵中 比x大的数字要超过 inx 
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){//sum[i][j]存的是小矩阵内 >= mid 的数量
			sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+(mp[i][j]>=x);
		}
	}
	for(int i=k;i<=n;i++){
		for(int j=k;j<=n;j++){//如果有小矩阵中 
			int tmp=sum[i][j]-sum[i-k][j]-sum[i][j-k]+sum[i-k][j-k];
			if(tmp < inx) return false;
		}
	}
	return true;
} 
int main(){
	scanf("%d%d",&n,&k);
	inx= k * k/2 + 1;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			scanf("%lld",&mp[i][j]);
			l=min(mp[i][j],l);
			r=max(mp[i][j],r);
		}
	}
	while(l<r){
		int mid=(l+r+1)>>1;
		if(check(mid))//猜 mid 值,然后去验证每个小矩阵中>=mid的数字个数是否合法
			l=mid;
		else 
			r=mid-1;
	}
	printf("%lld",l);//最小中位数值
//	while(l<=r){
//		int mid=(l+r)/2;
//		if(check(mid)) ans=mid,l=mid+1;
//		else r=mid-1;
//	}cout<<ans;
	
	
} 
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<vector>
#include<functional>
#include<algorithm>
#include<stdlib.h>
#include<string>
#include<string.h>
#define _USE_MATH_DEFINES
#include<math.h>
#include<deque>
#include<set>
#include<map>
#include<queue>
#include<list>
#include<iostream>
#include <bitset>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(auto i=a;i<b;i++)
#define rep2(i, a)for(auto i : a)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x, std::greater<int>())
#define vec_cnt(_a, _n) (upper_bound(all(_a), _n) - lower_bound(all(_a), _n))
#define vec_unique(_a) _a.erase(std::unique(all(_a)), _a.end());
#define vvec vector<vector<ll>>
#define INF (1ll << 60)
ll mod = 1000000007;
ll gcd(ll a, ll b) { return a % b == 0 ? b : gcd(b, a % b); }
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }
ll power(ll x, ll p) { ll a = 1; while (p > 0) { if (p & 1)a *= x; x *= x; p >>= 1; }return a; }
ll mpower(ll x, ll p) { ll a = 1; while (p > 0) { if (p & 1)a = a * x % mod;  x = x * x % mod; p >>= 1; }return a; }
ll co(ll n, ll k) { ll a = 1; rep(i, 1ll, k + 1) { a *= n - i + 1; a /= i; }return a; }
ll mc(ll n, ll m) { ll k = 1, l = 1; rep(i, n - m + 1, n + 1) k = k * i % mod; rep(i, 1, m + 1) l = l * i % mod; l = mpower(l, mod - 2); return k * l % mod; }
ll modinv(ll a) { ll b = mod, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); }u %= mod; if (u < 0) u += mod; return u; }

int main(void) {
	int n, k;
	cin >> n >> k;
	vector<vector<int>> a(n, vector<int>(n));
	int r = 0, l = 0;
	rep(i, 0, n)rep(j, 0, n)cin >> a[i][j], r = max(r, a[i][j]);
	while (r - l > 1) {
		int m = (r + l) / 2;
		vector<vector<int>> s(n + 1, vector<int>(n + 1));
		rep(i, 0, n) {
			rep(j, 0, n) {
				if (a[i][j] > m)s[i + 1][j + 1] = 1;
				s[i + 1][j + 1] += s[i][j + 1] + s[i + 1][j] - s[i][j];
			}
		}
		bool ok = false;
		rep(x, 0, n - k + 1) {
			rep(y, 0, n - k + 1) {
				if (s[x + k][y + k] - s[x][y + k] - s[x + k][y] + s[x][y] < k * k / 2 + 1) {
					ok = true;
					break;
				}
			}
			if (ok)break;
		}
		if (ok)r = m;
		else l = m;
	}
	printf("%d\n", r);
	return 0;
}