#include <bits/stdc++.h>

using namespace std;

#define eb emplace_back
#define ii pair<int, int>
#define OK (cerr << "OK" << endl)
#define debug(x) cerr << #x " = " << (x) << endl
#define ff first
#define ss second
#define int long long
#define tt tuple<int, int, int>
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vii vector<pair<int, int>>
#define vvi vector<vector<int>>
#define vvii vector<vector<pair<int, int>>>
#define Mat(n, m, v) vector<vector<int>>(n, vector<int>(m, v))
#define endl '\n'

constexpr int INF = (sizeof(int) == 4 ? 1e9 : 2e18) + 1e5;
constexpr int MOD = 1e9 + 7;
constexpr int MAXN = 2e5 + 3;

// INDEX BY ONE ALWAYS!!!
class BIT_2D {
private:
  // row, column
  const int n, m;
  vector<vector<int>> tree;

private:
  // Returns an integer which constains only the least significant bit.
  int low(const int i) { return i & (-i); }

  void _update(const int x, const int y, const int delta) {
    for (int i = x; i < n; i += low(i))
      for (int j = y; j < m; j += low(j))
        tree[i][j] += delta;
  }

  int _query(const int x, const int y) {
    int ans = 0;
    for (int i = x; i > 0; i -= low(i))
      for (int j = y; j > 0; j -= low(j))
        ans += tree[i][j];
    return ans;
  }

public:
  // put the size of the array without 1 indexing.
  /// Time Complexity: O(n * m)
  BIT_2D(const int n, const int m) : n(n + 1), m(m + 1) {
    tree.resize(this->n, vector<int>(this->m, 0));
  }

  /// Time Complexity: O(n * m * (log(n) + log(m)))
  BIT_2D(const vector<vector<int>> &mat)
      : n(mat.size()), m(mat.front().size()) {
    // Check if it is 1 indexed.
    assert(mat[0][0] == 0);
    tree.resize(n, vector<int>(m, 0));
    for (int i = 1; i < n; i++)
      for (int j = 1; j < m; j++)
        _update(i, j, mat[i][j]);
  }

  /// Query from (1, 1) to (x, y).
  ///
  /// Time Complexity: O(log(n) + log(m))
  int prefix_query(const int x, const int y) {
    assert(0 < x), assert(x < n);
    assert(0 < y), assert(y < m);
    return _query(x, y);
  }

  /// Query from (x1, y1) to (x2, y2).
  ///
  /// Time Complexity: O(log(n) + log(m))
  int query(const int x1, const int y1, const int x2, const int y2) {
    assert(0 < x1), assert(x1 <= x2), assert(x2 < n);
    assert(0 < y1), assert(y1 <= y2), assert(y2 < m);
    return _query(x2, y2) - _query(x1 - 1, y2) - _query(x2, y1 - 1) +
           _query(x1 - 1, y1 - 1);
  }

  /// Updates point (x, y).
  ///
  /// Time Complexity: O(log(n) + log(m))
  void update(const int x, const int y, const int delta) {
    assert(0 < x), assert(x < n);
    assert(0 < y), assert(y < m);
    _update(x, y, delta);
  }
};

vvi mat;
int n;
int k;

bool check(int mid) {
  vvi mt = Mat(n + 1, n + 1, 0);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      mt[i][j] = mat[i][j] <= mid;

  BIT_2D bit(mt);
  int sz = k * k / 2 + 1;
  if (k % 2 == 0)
    sz--;
  // debug(sz);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) {
      int x2 = i + k - 1;
      int y2 = j + k - 1;
      if (x2 > n || y2 > n)
        break;
      if (bit.query(i, j, x2, y2) >= sz) {
        return true;
      }
    }
  return false;
}

// #define MULTIPLE_TEST_CASES
void solve(const int test) {
  cin >> n;

  cin >> k;

  mat = Mat(n + 1, n + 1, 0);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) {
      cin >> mat[i][j];
    }

  int l = 0, r = 1e9;
  int ans = -1;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid)) {
      r = mid - 1;
      ans = mid;
    } else {
      l = mid + 1;
    }
  }
  assert(ans != -1);
  cout << ans << endl;
}

signed main() {
  // const string FILE_NAME = "";
  // freopen((FILE_NAME + string(".in")).c_str(), "r", stdin);
  // freopen((FILE_NAME + string(".out")).c_str(), "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);

  int t = 1;
#ifdef MULTIPLE_TEST_CASES
  cin >> t;
#endif
  for (int i = 1; i <= t; ++i)
    solve(i);
}

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

const int N = 810;
typedef long long ll;

int g[N][N];
int s[N][N];
int n, k;

bool check(int x)
{
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + (g[i][j] >= x);
            
    for(int i=k;i<=n;i++)
        for(int j=k;j<=n;j++)
        {
            int t = s[i][j] - s[i-k][j] - s[i][j-k] + s[i-k][j-k];
            if((k*k/2) + 1 > t) return 0;
        }
    return 1;
}
int main()
{
    cin >> n >> k;
    
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            cin >> g[i][j];
            
    int l = 0, r = 1e9;
    
    while(l < r)
    {
        int mid = (l + r + 1) >> 1;
        if(check(mid)) l = mid;
        else r = mid - 1;
        
    }
    cout << l;
}