#include<bits/stdc++.h>
#include<cmath>
#define pb push_back
#define ld long double
#define mp make_pair
#define vl vector<ll> 
#define vd vector<double>
#define vld vector<long double>
#define ll long long int
#define pl pair<ll, ll>
#define all(a) a.begin(), a.end()
#define forr(i, n) for(ll i=0; i<n; i++) 
#define forr1(i, n) for(ll i=1; i<=n; i++)
using namespace std;
const ld PI =3.1415926535897923846;
const ll MOD = 1000000007;
const ll N=998244353;
ll power(ll x,ll n){ll res=1;while(n>0){if(n&1) res=res*x%MOD;x=x*x%MOD;n>>=1;}return res;}
ll modinverse(ll a){return power(a, MOD-2);}
ll n, k;
bool bin_search(vector<vl> shite, ll n, ll ele)
{
	vector<vl> s(n+1, vl(n+1, 0));
	forr1(i, n)
	{
		forr1(j, n)
		{
			s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + shite[i-1][j-1];
		}
	}
	for(ll i=k; i<=n; i++)
	{
		for(ll j=k; j<=n; j++)
		{
			if (s[i][j]-s[i-k][j]-s[i][j-k]+s[i-k][j-k]>=ele)
			{
				return false;
			}
		}
	}
	return true;
}
void solve()
{
    cin>>n>>k;
    vector<vl> a(n, vl(n, 0));
    forr(i, n)
    {
    	forr(j, n)
    	{
    		cin>>a[i][j];
    	}
    }
    ll low=0, high=(ll)1e10;
    ll works=high;
    ll ele=(k*k-1)/2+1;
    while(high>=low)
    {
    	ll mid=low+(high-low)/2;
    	vector<vl> b(n, vl(n, 0));
    	forr(i, n)
    	{
    		forr(j, n)
    		{
    			if(a[i][j]<=mid) b[i][j]=1;
    			else b[i][j]=0;
    		}
    	}
    	if(!bin_search(b, n, ele))
    	{
    		high=mid-1;
    	}
    	else
    	{
    		low=mid+1;
    	}
    }
	cout<<low<<endl;
}
int main()
{
	#ifndef ONLINE_JUDGE
        // for getting input from input.txt
        freopen("input.txt", "r", stdin);
        // for writing output to output.txt
        freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll test=1;
    //cin>>test;
    while(test--)
    {
        solve();
    }
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}
// Qg3
// //g++ -std=c++17 -O2 -Wall a.cpp -o test
// _()_
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define vll vector<ll>
#define vi vector<int>
#define vb vector<bool>
#define pi pair<int,int>
#define pll pair<ll,ll>
#define vp vector<pi>
#define vpll vector<pll>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define For(i,a,b) for(ll i=a;i<b;i++)
#define debug2(x,y) cout<<"This side ----> "<<#x<<" -> "<<x<<" | "<<#y<<" -> "<<y<<endl;
#define debug(x) cout<<"This side    ----> "<<#x<<" -> "<<x<<endl
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(), x.rend()
#define mint map<int,int>
#define mall map<ll,ll>
#define ciN cin
#define gu(a,s) get<a>(s)
#define tin tuple<ll,ll,ll>
#define ter(x,y,z) ((x)?y:z)
#define endl "\n";

/////////////
const ll maxn = 1e5 + 3;
const ll max_val = 5e4 + 10;
ll mod = 1e9 + 7;
const ll bits = 18;
ll caseNumber = 1;
////////////////////////////////////////////////////////////////
int dp[26][maxn];
int n; string s;
int work(int lst, int ind) {
	if (ind >= n)return 1e8;
	if (ind == 0) {
		lst = s[ind] - 'a';
		return work(lst, ind + 1);
	}
	if (ind == n - 1) {
		if (lst != s[ind] - 'a') {
			return 1;
		}
		else {
			return 1e8;
		}
	}
	if (dp[lst][ind] != -1)return dp[lst][ind];
	else {
		int curr = 1e8;
		if (s[ind] - 'a' != lst) {
			int here = work(s[ind + 1] - 'a', ind + 1);
			if (here != -1) {
				curr = min(curr, here + 1);
			}
		}
		int ok = work(lst, ind + 1);
		if (ok != -1) {
			curr = min(curr, ok);
		}
		return dp[lst][ind] = curr;

	}
}
void solve() {
	cin >> n;
	cin >> s;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < 26; j++) {
			dp[j][i] = -1;
		}
	}
	if (s.size() == 1) {
		cout << -1 << endl; return;
	}
	int fin = work(0, 0);
	if (fin == 1e8) {
		cout << -1 << endl; return;
	}
	cout << fin << endl;

}


bool TestCase1 = 0;
bool isGoogles = 0;



//////////////////////////////////////////////////////////////////




//////////////////////////////////////////////////////////

int main() {
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);

	ios::sync_with_stdio(0);
	cin.tie(0);
	ll t;
	t = 1;



	if (TestCase1) {
		cin >> t;
	}
	for (int i = 0; i < t; i++) {
		solve();

	}
	return 0;
}
/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH

*/