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

#define Mod(x) (x>=P)&&(x-=P)
#define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i)
#define erep(i,a) for(int i=hd[a];i;i=nxt[i])

typedef long long ll;
void Max(int &x,int y){(x<y)&&(x=y);}
void Min(int &x,int y){(x>y)&&(x=y);}

bool vio;
char IO;
int rd(int res=0){
	bool f=0;
	while(IO=getchar(),IO<48||IO>57)
		f=IO=='-';
	do res=(res<<1)+(res<<3)+(IO^48);
	while(IO=getchar(),isdigit(IO));
	return f?-res:res;
}
const int M=1e3+10;
int C[M][M],A[M][M],B[M][M],n,K,S;
int Sc(int i,int j){
	return C[i][j]-C[i-K][j]-C[i][j-K]+C[i-K][j-K];
}
bool check(int x){
	rep(i,1,n)rep(j,1,n)C[i][j]=(A[i][j]<=x);
	rep(i,1,n)rep(j,1,n)C[i][j]+=C[i][j-1];
	rep(i,1,n)rep(j,1,n)C[i][j]+=C[i-1][j];
	rep(i,K,n)rep(j,K,n)
		if(Sc(i,j)>=S)return 1;
	return 0;
}
bool let;
int main(){
	cerr<<(&vio-&let)/1024.0/1024<<endl;
	n=rd(),K=rd(),S=K*K/2+1;
	S=K*K+1-S;
	rep(i,1,n)rep(j,1,n)A[i][j]=rd();
	int L=0,R=1e9,res=0;
	while(L<=R){
		int mid=(L+R)>>1;
		if(check(mid)){
			res=mid,R=mid-1;
		}else L=mid+1;
	}	
	printf("%d",res);
}

	
 #include <bits/stdc++.h>
 
 #define FIXED_FLOAT(x)  std::fixed <<std::setprecision(20) << (x)
 #define all(v) (v).begin(), (v).end()
 using namespace  std;
 #define forn(i,n) for (int i = 0; i < (n); ++i)
 #define rforn(i, n) for(int i = (n) - 1;i >= 0;--i)
 using ll = long long;
 int mod = (ll)1e9 + 7;
 #define PI acos(-1)
 typedef pair<int, int> pairs;
 
 const int INF = 1e9 + 1;
 const int N = 2e5 + 100;
 const double eps = 1e-7;
 
 template <class T> using V = vector<T>;  // from yosupo 
 template <class T> using VV = V<V<T>>;  // from yosupo
 
 template <typename XPAX>
 void ckma(XPAX &x, XPAX y) {
     x = (x < y ? y : x);
 }
 template <typename XPAX>
 void ckmi(XPAX &x, XPAX y) {
     x = (x > y ? y : x);
 }
 
 void __print(int x) {cerr << x;}
 void __print(long x) {cerr << x;}
 void __print(long long x) {cerr << x;}
 void __print(unsigned x) {cerr << x;}
 void __print(unsigned long x) {cerr << x;}
 void __print(unsigned long long x) {cerr << x;}
 void __print(float x) {cerr << x;}
 void __print(double x) {cerr << x;}
 void __print(long double x) {cerr << x;}
 void __print(char x) {cerr << '\'' << x << '\'';}
 void __print(const char *x) {cerr << '\"' << x << '\"';}
 void __print(const string &x) {cerr << '\"' << x << '\"';}
 void __print(bool x) {cerr << (x ? "true" : "false");}
 
 template<typename T, typename V>
 void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
 template<typename T>
 void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
 void _print() {cerr << "]\n";}
 template <typename T, typename... V>
 void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
 #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
 void solve() {
 
 
 	int n;
 	cin >> n;
 	int k;
 	cin >> k;

 	VV<int> c(n, V<int>(n));

 	struct node {
 		int i, j, x;
 		bool operator<(node p) {
 			return x < p.x;
 		};

 	};
 	V<node> val;
 	forn(i, n) forn(j, n) {
 		cin >> c[i][j];
 		node p;
 		p.x = c[i][j];
 		p.i = i;
 		p.j = j;
 		
 		val.push_back(p);
 	}
 	sort(all(val));
 	int G = k * k;
 	auto issok = [&](int now) {
 		VV<int> pref(n + 1, V<int>(n + 1));
 		for(auto c : val) {
 			if(c.x <= now) {
 				pref[c.i + 1][c.j + 1]++;
 			}
 			else break;
 		}
 		for(int i = 1;i <= n;++i) {
 			for(int j = 1;j <= n;++j) {
 				pref[i][j] += pref[i - 1][j] + pref[i][j - 1] - pref[i - 1][j - 1];
 			}
 		}
 		/*
 		for(int i = 1;i <= n;++i) {
 			for(int j = 1; j <= n;++j) {
 				cout << pref[i][j] << ' ';
 			}
 			cout << '\n';
 		}
 		*/
 		//debug(pref[n][n]);
 		int pk = 0;
 		for(int i = 1;i <= n;++i) {
 			for(int j = 1;j <= n;++j) {
 				int x = i + k - 1;
 				int y = j + k - 1;
 				if(x > n || y > n)continue;
 				int sum = pref[x][y] - pref[x][j - 1] - pref[i - 1][y] + pref[i - 1][j - 1];
 				//debug(i, j, x, y, sum);
 				//debug(pref[x][j - 1], pref[i][y - 1]);
 				ckma(pk, sum);
 				//debug(sum);
 			}
 		}

 		return (pk >= (G + 1) / 2);
 	};
 	
 	
 	int low = 0;
 	int high = 1e9;

 	while(low < high) {
 		int mid = (low + high) / 2;

 		if(issok(mid)) {
 			high = mid;
 		}
 		else low = mid + 1;
 	}
 	cout << low << '\n';

 
 }
 void test_case() {
     int t;
     cin >> t;
     forn(p, t) {
 
         //cout << "Case #" << p + 1 << ": ";
         solve();
     }
 }
 int main() {
 
     ios::sync_with_stdio(false);
     cin.tie(nullptr);
     cout.tie(nullptr);
 
     solve();
 
 }
  
