#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0, length = n; i < length; i++)
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ep emplace
#define epb emplace_back
#define scll static_cast<long long> 
#define sz(x) static_cast<int>((x).size())
#define pfll(x) printf("%lld\n", x)
#define ci(x) cin >> x
#define ci2(x, y) cin >> x >> y
#define ci3(x, y, z) cin >> x >> y >> z
#define ci4(w, x, y, z) cin >> w >> x >> y >> z
#define co(x) cout << x << endl
#define co2(x, y) cout << x << " " << y << endl
#define co3(x, y, z) cout << x << " " << y << " " << z << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> PQG;
typedef priority_queue<P> PQP;
typedef priority_queue<P, vector<P>, greater<P>> PQPG;

const int MAX_N = 2e5, MOD = 1e9 + 7, INF = 1e9;
int n, k, a[800][800];

int comp(int x) {
    int sum[n + 1][n + 1];
    rep(i, n + 1) sum[0][i] = 0;
    rep(i, n + 1) sum[i][0] = 0;
    rep(i, n) rep(j, n) sum[i + 1][j + 1] = sum[i][j + 1] + (a[i][j] >= x);
    rep(i, n - k + 1) {
        int tmp = 0;
        rep(j, k) tmp += sum[i + k][j] - sum[i][j];
        rep(j, n - k + 1) {
            tmp += sum[i + k][j + k] - sum[i][j + k] -
                sum[i + k][j] + sum[i][j];
            if (tmp < k * k / 2 + 1) return false;
        }
    }
    return true;
}
                
int main() {
    ci2(n, k);
    rep(i, n) rep(j, n) ci(a[i][j]);
    int l = 0, r = 1e9 + 1;
    while (r - l > 1) {
        int mid = (l + r) / 2;
        ((comp(mid)) ? l : r) = mid;
    }
    co(l);
    return 0;
}

#define rep(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

ll A[810][810];
ll B[810][810];
ll S[810][810];
ll n,k;
bool f(ll x){
  for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++){
      if(A[i][j]<=x) B[i][j]=1;
      else B[i][j]=0;
    }
  }
  for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++){
      S[i][j]=S[i][j-1]+B[i][j];
    }
  }
  for(int i=1;i<=n;i++){
    for(int j=1;j<=n;j++){
      S[i][j]+=S[i-1][j];
    }
  }
  bool b=false;
  for(int i=1;i+k-1<=n;i++){
    for(int j=1;j+k-1<=n;j++){
      if(S[i+k-1][j+k-1]-S[i+k-1][j-1]-S[i-1][j+k-1]+S[i-1][j-1]>=k*k-k*k/2){
        b=true;
        break;
      }
    }
    if(b) break;
  }
  
  if(b) return true;
  else return false;
}

int main(){
  cin>>n>>k;
  rep(i,n){
    rep(j,n) cin>>A[i+1][j+1];
  }
  
  ll ng=-1,ok=1e9+1;
  while(ok-ng>1){
    ll mid=(ng+ok)/2;
    if(f(mid)) ok=mid;
    else ng=mid;
  }
  cout<<ok<<endl;
  
  f(4);
  
  return 0;
}