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

typedef long long int ll;
#define All(a) (a).begin(),(a).end()
#define INF 2e18;
#define MOD 1000000007

int main(){
	ll n,m,ans,a,M=0;
	cin >> n>>m;
	vector<vector<ll>> v(n+1);
	for(ll i=0;i<n;i++){
		cin >> a;
		M=max(M,a);
		v[a].push_back(i);
	}
	for(ll i=0;i<=n;i++){
		if(v[i].empty()){
			cout << i << endl;
			return 0;	
		}
		//cout << v[i].front()<<" "<<v[i].back() << endl;
		if(n-m-1>=v[i].back() || v[i].front()>=m){
				cout <<i<<endl;
				return 0;
		}
		for(auto itr=v[i].begin();itr!=v[i].end()-1;itr++){
			if(*(itr+1)-*itr>m){
				//cout<<*(itr+1)<<" "<<*itr<<endl;
				cout << i << endl;
				return 0;
			}
		}
	}
	//cout << ans << endl;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

constexpr int N = 800;

int dp[N + 1][N + 1];
int a[N + 1][N + 1];
int n, k;

int query(int xi, int yi, int xf, int yf) {
	return dp[xf][yf] - dp[xi - 1][yf] - dp[xf][yi - 1] + dp[xi - 1][yi - 1];
}

bool check(int x) {
	int i, j;

	for (i = 1; i <= n; i++) {
		for (j = 1; j <= n; j++) {
			dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + (a[i][j] <= x);
		}
	}

	for (i = 1; i + k - 1 <= n; i++) {
		for (j = 1; j + k - 1 <= n; j++) {
			if (query(i, j, i + k - 1, j + k - 1) >= (k * k + 1) / 2) {
				return true;
			}
		}
	}

	return false;
}

int main() {
	int l, r, m, i, j;
	vector<int> v;

	scanf("%d%d", &n, &k);

	for (i = 1; i <= n; i++) {
		for (j = 1; j <= n; j++) {
			scanf("%d", a[i] + j);
			v.push_back(a[i][j]);
		}
	}

	sort(v.begin(), v.end());

	l = 0;
	r = v.size() - 1;

	while (l < r) {
		m = l + (r - l) / 2;

		if (check(v[m])) {
			r = m;
		}
		else {
			l = m + 1;
		}
	}

	printf("%d\n", v[l]);

	return 0;
}