#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <iomanip>

using namespace std;

typedef long long ll;

ll n, k;
ll yama[800][800];
vector<ll> heights;
ll centerNum;
ll num[800][800];

int main()
{
	cin >> n >> k;
	for (ll y = 0; y < n; y++) {
		for (ll x = 0; x < n; x++) {
			cin >> yama[x][y];
			heights.push_back(yama[x][y]);
		}
	}
	sort(heights.begin(), heights.end());

	centerNum = (k * k + 1) / 2;

	if (k == 1) {
		cout << heights[0] << endl;
		return 0;
	}

	ll l = 0;
	ll r = n * n - 1;

	while (r - l > 1) {
		ll center = (l + r) / 2;
		ll height = heights[center];
		for (ll y = 0; y < n; y++) {
			for (ll x = 0; x < n; x++) {
				if (yama[x][y] <= height) {
					num[x][y] = 1;
				}
				else {
					num[x][y] = 0;
				}
			}
		}

		for (ll y = 0; y < n; y++) {
			for (ll x = 0; x < n; x++) {
				if (0 < y) {
					num[x][y] += num[x][y - 1];
				}
				if (0 < x) {
					num[x][y] += num[x - 1][y];
				}
				if ((0 < x) && (0 < y)) {
					num[x][y] -= num[x - 1][y - 1];
				}

			}
		}

		bool isExist = false;
		for (ll y = 0; y + k <= n; y++) {
			if (isExist) break;
			for (ll x = 0; x + k <= n; x++) {
				ll areaNum = num[x + k - 1][y + k - 1];
				if (0 < x) {
					areaNum -= num[x - 1][y + k - 1];
				}
				if (0 < y) {
					areaNum -= num[x + k - 1][y - 1];
				}
				if ((0 < x) && (0 < y)) {
					areaNum += num[x - 1][y - 1];
				}

				if (centerNum <= areaNum) {
					isExist = true;
					break;
				}
			}
		}

		if (isExist) {
			r = center;
		}
		else {
			l = center;
		}

	}

	cout << heights[r] << endl;
}

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
void*wmem;
char memarr[96000000];
inline int my_getchar(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline int rd(char c[]){
  int i;
  int sz = 0;
  for(;;){
    i = my_getchar();
    if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
      break;
    }
  }
  c[sz++] = i;
  for(;;){
    i = my_getchar();
    if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF){
      break;
    }
    c[sz++] = i;
  }
  c[sz]='\0';
  return sz;
}
inline void rd(string &x){
  char*buf = (char *)wmem;
  rd(buf);
  x = buf;
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar('-');
  }
  while(s--){
    my_putchar(f[s]+'0');
  }
}
template<class S, class T> inline S chmin(S &a, T b){
  if(a>b){
    a=b;
  }
  return a;
}
int dp[100000+10];
int hist[128];
int n;
string s;
int main(){
  int i;
  wmem = memarr;
  memset(dp, 0x7f, sizeof(dp));
  memset(hist, 0x7f, sizeof(hist));
  rd(n);
  rd(s);
  if(n == 1){
    wt_L(-1);
    wt_L('\n');
    exit(0);
  }
  dp[0] = 0;
  hist[s[0]] = 0;
  for(i=(1);i<(n);i++){
    int j;
    for(j=('a');j<('z' + 1);j++){
      if(j == s[i]){
        continue;
      }
      chmin(dp[i], hist[j]+1);
    }
    if(i+1 < n){
      chmin(hist[s[i+1]], dp[i]);
    }
  }
  if(dp[n-1] <= 100000){
    wt_L(dp[n-1]);
    wt_L('\n');
  }
  else{
    wt_L(-1);
    wt_L('\n');
  }
  return 0;
}
// cLay version 20210405-1

// --- original code ---
// //no-unlocked
// int dp[1d5+10];
// int hist[128];
// int n;
// string s;
// { 
//     memset(dp, 0x7f, sizeof(dp));
//     memset(hist, 0x7f, sizeof(hist));
//     
//     rd(n,s);
//     if(n == 1){
//         wt(-1);
//         exit(0);
//     }
//     dp[0] = 0;
//     hist[s[0]] = 0;
//     rep(i, 1, n){
//         rep(j, 'a', 'z' + 1){
//             if(j == s[i]) continue;
//             dp[i] <?= hist[j]+1;
//         }
//         if(i+1 < n) hist[s[i+1]] <?= dp[i];
//     }
//     if(dp[n-1] <= 1d5){
//         wt(dp[n-1]);
//     }else{
//         wt(-1);
//     }
// }