
//#include <atcoder/maxflow.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
 

#include <iostream>
#include <map>
#include <list>
#include <set>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <queue>
#include <deque>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <iterator>
#include <random>
#include <chrono>
#include <complex>
#include <bitset>
#include <fstream>
 
 
#define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i) int i; cin >> i
#define readll(i) ll i; cin >> i
#define readdouble(i) double i; cin >> i
#define readstring(s) string s; cin >> s

typedef long long ll;

 
//using namespace __gnu_pbds;
//using namespace atcoder;
using namespace std;

const ll modd = (1000LL * 1000LL * 1000LL + 7LL);
//const ll modd = 998244353;


ll binary_search(function<bool(ll)> func, ll start, ll end) {
    /*      func:int ->bool
            returns smallest int x where func(x) evaluates to true, searches in [start,end), it is assumed the values are false, .. , false, true ...
             */
    if (end <= start) {   return end;  }   // has to be here, otherwise func(end-1) in next line could be a problem
    if (!func(end-1)) {  return end;  }
    while (end-start>1) {
        ll mid = (start+end)/2;
        if (func(mid)) {  end = mid;  } else { start = mid;   }
    }
    if (func(start)) {  return start;  } else {  return end;   }
};

int main(int argc, char *argv[])   {

    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<int> rand_gen(0, modd);   // rand_gen(rng) gets the rand no

    ios_base::sync_with_stdio(false);
    cin.tie(0);
 
    cout.precision(12);

//    readint(test_cases);
    int test_cases = 1;
    forr(t, 1, test_cases) {
        readint(n); readint(k);
        vector<vector<ll>> a;
        ll minn = modd*modd;
        forr(i,0,n) {
            a.push_back(vector<ll>());
            forr(j,0,n) {
                readll(aa);
                minn = min(minn, aa);
                a.back().push_back(aa);
            }
        }

        int bel = k*k - (k*k)/2;

        ll x = binary_search([&a,bel,k,n](ll med) {
            vector<vector<ll>> aa(n, vector<ll>(n, 0));
            forr(i,0,n) {
                forr(j,0,n) {
                    aa[i][j] = (a[i][j]<=med);
                }
            }
            vector<vector<ll>> summ(n+1, vector<ll>(n+1, 0));
            forr(i,1,n) {
                forr(j,1,n) {
                    summ[i][j] = -summ[i-1][j-1] + summ[i][j-1] + summ[i-1][j] + aa[i-1][j-1];
                }
            }

            bool ret = false;
            forr(i,0,n-k+1) {
                forr(j,0,n-k+1) {
                    ret |= (summ[i+k][j+k]-summ[i][j+k]-summ[i+k][j]+summ[i][j]>=bel);
                }
            }

            return ret;
        }, 0, modd);

        cout << x << endl;


    }

    return 0;
}
#include <bits/stdc++.h>

using namespace std;

vector<vector<int>> acum;

bool check_median(int median, int K,
		  vector<vector<int>> &A){
  int N = A.size() - 1;
  acum.assign(N+1, vector<int>(N+1, 0));

  for(int i = 1; i <= N; i++){
    for(int j = 1; j <= N; j++){
      acum[i][j] = A[i][j] <= median;
      acum[i][j] += acum[i][j-1] + acum[i-1][j] - acum[i-1][j-1];
    }
  }

  for(int i = K; i <= N; i++){
    for(int j = K; j <= N; j++){
      int cnt_less = acum[i][j];
      cnt_less -= acum[i-K][j] + acum[i][j-K] - acum[i-K][j-K];

      if(cnt_less >= (K*K + 1)/2){
	// printf("(%d, %d) -- %d\n", i, j, cnt_less);
	return true;
      }
    }
  }

  return false;
}

int main(){
  int N, K;
  while(scanf("%d%d", &N, &K) > 0){
    vector<vector<int>> A(N+1, vector<int>(N+1));

    for(int i = 1; i <= N; i++){
      for(int j = 1; j <= N; j++){
	scanf("%d", &A[i][j]);
      }
    }

    int lo = 0, up = 1e9, ans;

    while(lo <= up){
      int mid = (lo + up) >> 1;

      if(check_median(mid, K, A)){
	ans = mid;
	up = mid-1;
      } else {
	lo = mid+1;
      }
    }

    printf("%d\n", ans);
  }
}
