#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define pb push_back
#define ll long long
#define Mid ((L+R)/2)
#define pi pair<int,int>
#define pii pair<pi,int>
#define deb(x) cout<<#x<<"="<<x<<endl
#define go ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
int n,k;
string s;
const int K = 18;
const int N = 2e5 + 7;
bool Ok(int x)
{
    vector <vector <int> > nxt(n+1,vector <int> (k,N));
    for(int j=0;j<k;j++)
    {
        int last=n;
        for(int i=n-1;i>=0;i--)
        {
            if(s[i]!='?' && s[i]-'a'!=j)
                last=i;
            nxt[i][j]=nxt[i+1][j];
            if(i+x<=last)
                nxt[i][j]=i;
        }
    }
    vector <int> dp(1<<k,N);
    dp[0]=0;
    for(int msk=1;msk<1<<k;msk++)
    {
        for(int j=0;j<k;j++)
        {
            if(!(msk & (1<<j))) continue;
            if(dp[msk ^ 1<<j]>=n) continue;
            dp[msk] = min(dp[msk],nxt[dp[msk ^ 1<<j]][j]+x);
        }
    }
    return dp[(1<<k)-1]<=n;
}
int main()
{
    go;
    cin>>n>>k>>s;
    int L=0,R=n,ans;
    while(L<=R)
    {
        if(Ok(Mid))
        {
            ans=Mid;
            L=Mid+1;
        }
        else
            R=Mid-1;
    }
    cout<<ans;
    return 0;
}
