#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include<cstdio>
#include<functional>
#include <bitset>
#include <iomanip>
#include <cctype>
#include <list>
#include <cassert>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for(ll i = n; i >= 0; i--)
#define ll long long
#define repi(i,a,b) for(ll i=a;i<b;++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()

using namespace std;
template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;
using vvv = vector<vector<vector<ll>>>;
using vv = vector<vector<ll>>;
using vec = vector<ll>;
using P = pair<ll, ll>;

bool isOK(ll n,ll k, vv al, ll mid) {
	vv tmp1(n, vec(n));
	rep(i, n) {
		rep(j, n) {
			if (al[i][j] <= mid) {
				tmp1[i][j] = 1;
			}
		}
	}
	vv tmp2(n, vec(n));
	rep(i, n) {
		rep(j, n) {
			if(j - 1 >= 0) tmp2[i][j] = tmp1[i][j] + tmp2[i][j - 1];
			else tmp2[i][j] = tmp1[i][j];
		}
	}
	rep(j, n) {
		rep(i, n) {
			if (i - 1 >= 0) tmp2[i][j] = tmp2[i][j] + tmp2[i - 1][j];
		}
	}
	ll cnt = -1;
	rep(i, n) {
		rep(j, n) {
			if (i + k >= n || j + k >= n) break;
			ll r = tmp2[i + k][j + k];
			if (i - 1 >= 0) {
				r -= tmp2[i - 1][j + k];
			}
			if (j - 1 >= 0) {
				r -= tmp2[i + k][j - 1];
			}
			if (i - 1 >= 0 && j - 1 >= 0) {
				r += tmp2[i - 1][j - 1];
			}
			cnt = max(cnt, r);
		}
	}
	if (((k + 1) * (k + 1)) % 2 && cnt >= (k + 1) * (k + 1) / 2 + 1) return true;
	else if (((k + 1) * (k + 1)) % 2 == 0 && cnt >= (k + 1) * (k + 1) / 2) return true;
	else return false;
}

int main(void) {
	ll n, k; cin >> n >> k;
	vv al(n, vec(n));
	ll mv = 0;
	rep(i, n) {
		rep(j, n) {
			ll a; cin >> a;
			mv = max(mv, a);
			al[i][j] = a;
		}
	}
	int ng = -1;
	int ok = mv;
	while (abs(ok - ng) > 1) {
		int mid = (ok + ng) / 2;
		if (isOK(n, k - 1,al, mid)) ok = mid;
		else ng = mid;
	}
	cout << ok << endl;
	system("pause");
}

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(n),b(n);
  rep(i,n) cin >> a[i] >> b[i];

  vector<int> p(2*n,-1),l(2*n,-1),r(2*n,-1);
  vector<bool> v(2*n+1);
  v[0] = [&]{
    rep(i,n){
      if(a[i]!=-1){
        --a[i];
        if(l[a[i]]!=-1||r[a[i]]!=-1) return false;
        l[a[i]]=i;
      }
      if(b[i]!=-1){
        --b[i];
        if(l[b[i]]!=-1||r[b[i]]!=-1) return false;
        r[b[i]]=i;
      }
      if(a[i]!=-1&&b[i]!=-1){
        if(b[i]<=a[i]) return false;
        p[a[i]] = b[i];
        p[b[i]] = a[i];
      }
    }
    return true;
  }();
  auto f = [&](int a,int b){
    if(r[a]!=-1) return false;
    if(l[b]!=-1) return false;
    if(l[a]!=-1&&r[b]!=-1&&l[a]!=r[b]) return false;
    if(p[a]!=-1&&p[a]!=b) return false;
    if(p[b]!=-1&&p[b]!=a) return false;
    return true;
  };
  for(int p=0;p<2*n;p+=2){
    if(!v[p]) continue;
    for(int d=1;p+2*d<=2*n;++d){
      if(v[p+2*d]) continue;
      v[p+2*d] = [&]{
        rep(i,d) if(!f(p+i,p+d+i)) return false;
        return true;
      }();
    }
  }
  cout << (v[2*n]?"Yes":"No") << endl;
}