#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 7;
const int inf = 1e9 + 7;
int a[807][807],b[807][807];
int n,k;

bool check(int m){
	for(int i = 1;i <= n;i++){		
		for(int j = 1;j <= n;j++){
			if(a[i][j] > m) b[i][j] = b[i-1][j]-1;
			else b[i][j] = b[i-1][j]+1;
		}
	}
	for(int i = 1;i <= n+1-k;i++){
		int st = i+k-1,ans = 0;
		for(int j = 1;j < k;j++){
			ans = ans + b[st][j] - b[i-1][j];
		}
		for(int j = k;j <= n;j++){
			ans += b[st][j] - b[i-1][j];
			if(ans >= 0) return 0;
			ans -= b[st][j+1-k] - b[i-1][j+1-k];
		}
	}
	return 1;
}
int main(){	
	
	scanf("%d %d",&n,&k);

	for(int i = 1;i <= n;i++){		
		for(int j = 1;j <= n;j++){
			scanf("%d",&a[i][j]);
		}
	}
	int l = 0,r = inf;
	while(l < r){
		int m = (l + r) / 2;
		if(check(m)){
			l = m + 1; 
		}else r = m;
	}
	printf("%d\n",l);
	return 0; 	
}

#include <bits/stdc++.h>

#define ll long long
#define map unordered_map
#define set unordered_set
#define l_l pair<ll, ll>
#define P pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>
#define mp make_pair
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rev(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define revs(i, n) for (int i = ((int)(n)); i > 0; --i)
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
// template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

using namespace std;

const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);

// template <class T> void plus_mod(T &a, const T &b) {a = (a + b) % MOD;}

ll N, K;

ll grid[999][999];

class Cumu {
 private:
  vector<ll> _sum;

 public:
  Cumu(vector<ll> &source) {
    _sum = vector<ll>(source.size() + 1);
    for (ll i = 1; i < source.size() + 1; i++) {
      _sum[i] = _sum[i - 1] + source[i - 1];
    }
  }
  ll sum(ll left, ll right) {
    //
    return _sum[right + 1] - _sum[left];
  }
};

ll count_equal_or_less_than(ll th) {
  // return 0;

  ll start_y = 0;

  ll max_count = 0;

  // 前計算
  vector<Cumu> cumu_list;
  rep(x, N) {
    vll zo_list;
    rep(y, N) {
      ll v = grid[y][x];

      zo_list.emplace_back(v <= th);
    }

    cumu_list.emplace_back(Cumu(zo_list));
  }

  // 本計算
  ll count = 0;

  for (;;) {
    ll end_y = start_y + K - 1;
    if (end_y >= N) break;

    // 最初の四角のカウント
    count = 0;
    rep(x, K) {
      ll c = cumu_list[x].sum(start_y, end_y);
      count += c;
    }
    chmax(max_count, count);

    // 右まで移動
    ll start_x = 0;
    for (;;) {
      ll end_x = start_x + K - 1;
      if (end_x == N - 1) break;  // 右端

      count -= cumu_list[start_x].sum(start_y, end_y);
      count += cumu_list[end_x + 1].sum(start_y, end_y);

      chmax(max_count, count);

      start_x += 1;
    }

    // 下に移動
    start_y += 1;
  }

  return max_count;
}

int main() {
  // std::cout << std::fixed << std::setprecision(10);

  scanf("%lld %lld", &N, &K);

  rep(y, N) {
    rep(x, N) {
      ll v;
      scanf("%lld", &v);
      grid[y][x] = v;
    }
  }

  ll nth = (K * K / 2) + 1;
  ll count_th = (K * K) - nth + 1;

  // leftが条件を満たしていないケースに注意
  ll left = -100;
  ll right = 2000000000LL;  // ■TODO

  for (;;) {
    // cout << "left:" << (left) << ",right:" << (right) << endl;

    if (1 >= right - left) {
      break;
    }
    ll center = (left + right) / 2;
    ll count = count_equal_or_less_than(center);

    if (count < count_th) {
      left = center;
    } else {
      right = center;
    }
  }

  // cout << "left:" << (left) << ",right:" << (right) << endl;

  ll ans = right;
  cout << (ans) << endl;

  // // [1, 3, 5, 6] -> 2
  // ll searchInsert(vector<ll>& nums, ll target) {
  //   ll left = 0;
  //   ll right = nums.size();
  //   for (;;) {
  //     if (1 == right - left) {
  //       if (nums[left] < target) {
  //         left = right;
  //       } else {
  //         right = left;
  //       }
  //       break;
  //     }
  //     if (0 >= right - left) {
  //       break;
  //     }
  //     ll center = (left + right) / 2;
  //     ll n = nums[center];
  //     if (n < target) {
  //       left = center;
  //     }
  //     if (n >= target) {
  //       right = center;
  //     }
  //   }
  //   return left;
  // }

  // ll ans = 0;

  // cout << ans << endl;
}
