#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#ifdef ENVIRONMENT_LINKED_BOOST
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#endif

using namespace std;
using uint  = unsigned int;
using ll    = long long;
using ull   = unsigned long long;
using lpair = std::pair<ll, ll>;
#define REP(i, a, b)    for (ll i = a; i < b; ++i)
#define REPREV(i, a, b) for (ll i = a; i > b; --i)

const int _ = []() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(10);
    return 0;
}();

template <typename value_t>
void resize(value_t& v, const value_t& val) { v = val; }
template <typename vec_t, typename value_t, typename... arg_t>
void resize(std::vector<vec_t>& v, const value_t& val, int size, arg_t... arg) {
    v.resize(size);
    for (auto& c : v) resize(c, val, arg...);
}
template <typename A, typename B>
void chmin(A& a, const B& b) { a = min(a, static_cast<A>(b)); };
template <typename A, typename B>
void chmax(A& a, const B& b) { a = max(a, static_cast<A>(b)); };

int main() {
    int N, K;
    cin >> N >> K;
    vector<array<int, 3>> A;
    REP(i, 0, N) {
        REP(j, 0, N) {
            int a;
            cin >> a;
            A.push_back({a, (int)i, (int)j});
        }
    }
    sort(A.begin(), A.end(), greater<array<int, 3>>());
    vector<vector<int>> sum(N + 1, vector<int>(N + 1, 0));
    int l = -1;
    int r = N * N - 1;
    while (r - l > 1) {
        for (auto& v : sum) {
            for (int& x : v) x = 0;
        }
        int mid = l + (r - l) / 2;
        REP(i, 0, mid + 1) {
            auto [a, x, y] = A[i];
            int px         = max(0, x - K + 1);
            int py         = max(0, y - K + 1);
            sum[px][py] += 1;
            sum[px][y + 1] -= 1;
            sum[x + 1][py] -= 1;
            sum[x + 1][y + 1] += 1;
        }
        REP(i, 0, N + 1) {
            REP(j, 1, N + 1) {
                sum[i][j] += sum[i][j - 1];
            }
        }
        REP(i, 1, N + 1) {
            REP(j, 0, N + 1) {
                sum[i][j] += sum[i - 1][j];
            }
        }
        bool ok = false;
        REP(i, 0, N - K + 1) {
            REP(j, 0, N - K + 1) {
                if (sum[i][j] < K * K / 2 + 1) {
                    ok = true;
                    break;
                }
            }
            if (ok) break;
        }
        (ok ? l : r) = mid;
    }
    cout << A[r][0] << endl;
    return 0;
}
#include <cmath>
#include <cstdio>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 807
using namespace std;
typedef long long ll;

inline int rd() {
    int x = 0;
    bool f = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) f |= (c == '-');
    for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48);
    return f ? -x : x;
}

int n, k, lim, a[N][N], sum[N][N];

inline bool valid(int x) {
    memset(sum, 0, sizeof(sum));
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j) {
            if (a[i][j] <= x) ++sum[i][j];
            sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
            if (i >= k && j >= k) {
                if (sum[i][j] - sum[i - k][j] - sum[i][j - k] + sum[i - k][j - k] >= lim) return 1;
            }
        }
    return 0;
}

int main() { 
    n = rd(); k = rd();
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= n; ++j) a[i][j] = rd();
    lim = k * k / 2 + (k & 1);
    int l = 0; 
    int r = (1 << 30);
    while (l < r) {
        int mid = (l + r) / 2;
        if (valid(mid)) r = mid;
        else l = mid + 1;
    }
    printf("%d\n", l);
    return 0;
}