#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<cstring>
#include<algorithm>
#include<climits>
using namespace std;
#define int long long
#define pii pair<int,int>
#define IOFast() ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
int n,k;
string s;
int res=0;
void init(){
	cin>>n>>k>>s;
}
bool check(int d){
	vector<int> lst(k,n);
	vector<vector<int> > pos(n+1,vector<int>(k,n+1));
	for(int i=n-1;i>=0;i--){
		if(s[i]!='?'){
			lst[s[i]-'a']=i;
		}
		int cur=n;
		for(int j=0;j<k;j++){
			pos[i][j]=(i+d<=cur? i+d: pos[i+1][j]);
			cur=min(cur,lst[j]);
		}
		cur=n;
		for(int j=k-1;j>=0;j--){
			if(i+d>cur)pos[i][j]=pos[i+1][j];
			cur=min(cur,lst[j]);
		}
	}
	vector<int> dp(1<<k,n+1);
	dp[0]=0;
	for(int i=0;i<(1<<k);i++){
		if(dp[i]<n+1){
			for(int j=0;j<k;j++){
				if(!((i>>j)&1))
					dp[i|(1<<j)]=min(dp[i|(1<<j)],pos[dp[i]][j]);
			}
		}
	}
	return dp[(1<<k)-1]<=n;
}
void solve(){
	int l=1,r=n;
	while(l<=r){
		int mid=(l+r)/2;
		if(check(mid)){
			res=mid;
			l=mid+1;
		}
		else{
			r=mid-1;
		}
	}
	cout<<res;
}
signed main(){
	IOFast();
	init();
	solve();
}
