#include <iostream>
using namespace std;

#define N 805
#define MAX_A 1000000000
#define rep(i, n) for(int i = 0; i < n; ++i)

int main() {
	int n, k, lim;
	int a[N][N];
	int s[N][N];
	rep(i, N) {
		s[i][0] = 0;
		s[0][i] = 0;
	}
	int ng = -1;
	int ok = MAX_A;
	int mid;
	bool ext;
	cin >> n >> k;
	lim = ((k * k) / 2) + 1;
	rep(i, n) {
		rep(j, n) {
			cin >> a[i][j];
		}
	}

	while ((ng + 1) < ok) {
		mid = (ng + ok) / 2;
		rep(i, n) {
			rep(j, n) {
				s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j];
				if (a[i][j] > mid)
					s[i + 1][j + 1]++;
			}
		}
		ext = false;
		rep(i, n - k + 1) {
			rep(j, n - k + 1) {
				if ((s[i + k][j + k] + s[i][j] - s[i][j + k] - s[i + k][j]) < lim)
					ext = true;
			}
		}
		if (ext)
			ok = mid;
		else
			ng = mid;

	}
	cout << ok << endl;
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair <int, int> pin;

const int N = 805;
const int M = 7e5 + 5;
const int Maxn = 1e9;
const ll P = 998244353LL;
const int inf = 1e9 + 5;

int n, k, pos, a[N][N], sum[N][N], ans = inf; 

template <typename T>
inline void read(T &X) {
    char ch = 0; T op = 1; 
    for (X = 0; ch > '9' || ch < '0'; ch = getchar())
        if (ch == '-') op = -1;
    for (; ch >= '0' && ch <= '9'; ch = getchar())
        X = (X * 10) + ch - '0';
    X *= op;
}

inline int getSum(int x1, int y1, int x2, int y2) {
    return sum[x2][y2] - sum[x2][y1 - 1] - sum[x1 - 1][y2] + sum[x1 - 1][y1 - 1];
}

inline bool chk(int mid) {
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) {
            sum[i][j] = 0;
            if (a[i][j] <= mid) sum[i][j] = 1;
        }
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            sum[i][j] += sum[i][j - 1];
    for (int j = 1; j <= n; j++)
        for (int i = 1; i <= n; i++)
            sum[i][j] += sum[i - 1][j];
    for (int i = 1; i <= n - k + 1; i++)
        for (int j = 1; j <= n - k + 1; j++)
            if (getSum(i, j, i + k - 1, j + k - 1) >= pos)
                return 1;
    return 0;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("sample.in", "r", stdin);
    clock_t st_clock = clock();
#endif

    read(n), read(k);
    int m = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) 
            read(a[i][j]);
    pos = k * k - (k * k / 2);
    int l = 0, r = inf, mid, res;
    for (; l <= r; ) {
        mid = (l + r) / 2;
        if (chk(mid)) res = mid, r = mid - 1;
        else l = mid + 1;
    }
    printf("%d\n", res);

#ifndef ONLINE_JUDGE
    clock_t ed_clock = clock();
    printf("time = %f ms\n", (double)(ed_clock - st_clock) / CLOCKS_PER_SEC * 1000);
#endif
    return 0;
}