#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0;i<(n);++i)
using ll = long long;
using P = pair<int, int>;
using C = complex<double>;

int main() {
	int n, k;
	cin >> n >> k;
	vector<vector<int>> a(n, vector<int>(n));
	rep(i, n)rep(j, n) cin >> a.at(i).at(j);
	int L = k * k / 2 + 1;

	int wa = -1, ac = 1001001001;
	while (wa + 1 < ac) {
		int wj = (wa + ac) / 2;
		bool ok = false;
		{
			vector<vector<int>> s(n + 1, vector<int>(n+1));
			rep(i, n)rep(j, n) s.at(i + 1).at(j + 1) = a.at(i).at(j)>wj?1:0;
			rep(i, n + 1)rep(j, n) s.at(i).at(j + 1) += s.at(i).at(j);
			rep(i, n)rep(j, n + 1) s.at(i + 1).at(j) += s.at(i).at(j);
			rep(i, n - k + 1)rep(j, n - k + 1) {
				int now = s.at(i + k).at(j + k);
				now -= s.at(i).at(j + k);
				now -= s.at(i + k).at(j);
				now += s.at(i).at(j);
				if (now < L) ok = true;
			}
		}

		if (ok) ac = wj; else wa = wj;
	}
	cout << ac << endl;
}
#include "bits/stdc++.h"
 
using namespace std;
 
#define ll long long
 
#define       forn(i,n)              for(int i=0;i<n;i++)
#define          all(v)              v.begin(), v.end()
#define         rall(v)              v.rbegin(),v.rend()
 
#define            pb                push_back
#define          sz(a)               (int)a.size()
#define         fastio               ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define      GR(a,n,m)               vector<vector<int>> a(n, vector<int>(m, 0));

#define int long long

const int N = 1000;
int c[N][N];
int a[N][N];
int s[N][N];

int query(int i1,int j1,int i2,int j2){
    int ans = s[i2][j2];
    if(i1)ans -= s[i1-1][j2];
    if(j1)ans -= s[i2][j1-1];
    if(i1&&j1)ans += s[i1-1][j1-1];
    return ans;
}
void solve()
{
    int n, k;
    cin >> n >> k;
    for(int i = 0;i < n;i++){
        for(int j = 0;j < n;j++){
            cin >> c[i][j];
        }
    }
    int l = 0,r = 1e9;
    int ans = r;

    int pos = (k * k - 1) / 2 + 1;
    while(l <= r)
    {
        int mid = l + (r - l) / 2;

        for(int i = 0;i < n;i++){
            for(int j = 0;j < n;j++){
                if(c[i][j] <=  mid){
                    a[i][j] = 1;
                }else{
                    a[i][j] = 0;
                }
            }
        }

        for(int i = 0;i < n;i++){
            s[i][0] = a[i][0];
            if(i)s[i][0] += s[i-1][0];
            s[0][i] = a[0][i];
            if(i)s[0][i]+=s[0][i-1];
        }
        for(int i = 1;i < n;i++){
            for(int j = 1;j < n;j++){
                s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
            }
        }
        bool ok=false;
        for(int i = 0;i + k - 1 < n;i++){
            for(int j = 0;j + k - 1 < n;j++){
                int sum = query(i,j,i+k-1,j+k-1);
                if(sum >= pos)ok=1;
            }
        }
        if(ok){
            ans=mid;
            r=mid-1;
        }else l=mid+1;
    }
    cout<<ans;
}   

int32_t main()
{
    fastio;
    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
}