#include <bits/stdc++.h>

#define ll long long
#define rep(j,i,n) for(int i=j;i<n;i++)

using namespace std;

int main()
{
	cin.tie(0); ios_base::sync_with_stdio(0); 
		
	int n,k; cin>>n>>k;
	
	vector<vector<ll>> v(n,vector<ll>(n));
	rep(0,i,n)rep(0,j,n) cin>>v[i][j];
	
	ll l = -1, r = 1e9 + 10;
	while (r - l > 1)
	{
		ll mid = (r + l) / 2;

		vector<vector<ll>> csum(n+1,vector<ll>(n+1, 0));
		rep(0,i,n)rep(0,j,n)
			csum[i+1][j+1] = csum[i+1][j] + csum[i][j+1] - csum[i][j] + (v[i][j] > mid);

		int cnt;
		rep(0,i,n-k+1)rep(0,j,n-k+1)
		{
			cnt = csum[k+i][k+j] - csum[k+i][j] - csum[i][j+k] + csum[i][j];
			if (cnt < ((k * k) / 2 + 1))
				r = mid;	
		}
		if (r != mid)
			l = mid;
	}
	cout << r << "\n";
}


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<functional>
using namespace std;
#define MOD 1000000007
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define PI 3.14159265358979323846

int arr[801][801];
int dp[801][801];

int main()
{
	int n, k, left, right, mid, ans, temp;
	bool check;
	scanf("%d %d", &n, &k);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			scanf("%d", &arr[i][j]);
	temp = k * k / 2;
	ans = 1e9;
	left = 0;
	right = 1e9;
	while (left <= right)
	{
		mid = (left + right) / 2;
		memset(dp, 0, sizeof(dp));
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
			{
				dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
				if (arr[i][j] > mid)
					dp[i][j]++;
			}
		check = false;
		for (int i = k; i <= n; i++)
			for (int j = k; j <= n; j++)
			{
				if (dp[i][j] - dp[i - k][j] - dp[i][j - k] + dp[i - k][j - k] <= temp)
					check = true;
			}
		if (check)
		{
			ans = min(ans, mid);
			right = mid - 1;
		}
		else
			left = mid + 1;
	}
	printf("%d\n", ans);
	return 0;
}