//Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <cassert>
#include <iomanip>
#include <chrono>
#include <random>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
#define int long long
#define L __int128
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define a first
#define b second
#define fi first
#define sc second
#define rng(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
#define si(x) int(x.size())
 
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
 
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
 
template<class t> using vc=vector<t>;
 
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.fi<<","<<p.sc<<"}";
}
 
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}
 
template<class T>
void g(T &a){
	cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 1000000007;//998244353
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
template<class T>
void add(T&a,T b){
	a+=b;
	if(a >= mod) a-=mod;
}
ll modpow(ll x,ll n){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return res;
}
#define sz 1
ll F[sz],R[sz];
void make(){
	F[0] = 1;
	for(int i=1;i<sz;i++) F[i] = F[i-1]*i%mod;
	for(int i=0;i<sz;i++) R[i] = modpow(F[i],mod-2);
}
ll C(int a,int b){
	if(b < 0 || a < b) return 0;
	return F[a]*R[b]%mod*R[a-b]%mod;
}
int a[805][805],rui[805][805];
void solve(){
	int n, k;
	cin>>n>>k;;
	repn(i,n)repn(j,n)cin>>a[i][j];
	int lb=-1,ub=1e9;
	while(ub-lb>1){
		int mid=(lb+ub)/2;
		repn(i,n)repn(j,n)rui[i][j]=a[i][j]<=mid;
		repn(i,n)repn(j,n)rui[i][j]=rui[i][j]+rui[i-1][j]+rui[i][j-1]-rui[i-1][j-1];
		int ok = 0;
		repn(i,n-k+1)repn(j,n-k+1){
			int sum = rui[i+k-1][j+k-1];
			sum -= rui[i-1][j+k-1] + rui[i+k-1][j-1];
			sum += rui[i-1][j-1];
			
			if(sum >= (k*k+1)/2) ok = 1;
		}
		if(ok)ub=mid; else lb=mid;
	}
	o(ub);
}
signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	int t; t = 1; //cin >> t;
	while(t--) solve();
}

#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std;
const int N = 1e5 + 5;
struct elm{
    int a, b, c;
    bool operator < (const elm &other){
        return c < other.c;
    }
};
int main(){
    ios::sync_with_stdio(0), cin.tie(0);
    int n, k, p;
    cin >> n >> k;
    p = k * k - (k * k / 2);
    vector<elm> x;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            int inp;
            cin >> inp;
            x.push_back({i, j, inp});
        }
    }
    sort(x.begin(), x.end());
    int l = 0, r = n * n - 1;
    while (l < r){
        int mid = l + r >> 1, can = 0;
        int has[n][n]{}, pre[n][n + 1]{};
        for (int i = 0; i <= mid; i++) has[x[i].a][x[i].b] = 1;
        for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) pre[i][j + 1] = pre[i][j] + has[i][j];
        for (int j = 0; j <= n - k; j++){
            int tmp = 0;
            for (int i = 0; i < n; i++){
                tmp += pre[i][j + k] - pre[i][j];
                if (i >= k) tmp -= pre[i - k][j + k] - pre[i - k][j];
                if (tmp >= p) can = 1;
                //cout << mid << ' ' << tmp << '\n';
            }
        }
        if (can) r = mid;
        else l = mid + 1;
    }
    cout << x[l].c;
}