#include<bits/stdc++.h>

using namespace std;
#define DB
#ifdef DB
#define el cerr << "\n";
#define db(...) cerr << " [" << #__VA_ARGS__ << " : " << __VA_ARGS__ << "] ";
#else
#define el
#define db(...)
#endif // DB

const int INF = 1e9;
const int N = 1e6 + 5;
int dp[N];
int pos[20][N];
int n, k;
string s;
int bmk;

bool can(int i, int ch) {
    return s[i] == '?' or s[i] - 'a' == ch;
}

void init(int ch, int M) {
    int cnt = 0;
    for (int i = 0; i < M; ++i) {
        pos[ch][n - i] = INF;
        if (can(n - i - 1, ch)) ++cnt;
    }
    pos[ch][n - M] = (cnt == M ? n - M : INF);
    for (int i = n - M - 1; i >= 0; --i) {
        if (can(i + M, ch)) --cnt;
        if (can(i, ch)) ++cnt;
        pos[ch][i] = (cnt == M ? i : pos[ch][i + 1]);
    }
}

bool ok(int M) {
    if (M == 0) return true;
    for (int i = 0; i < k; ++i) {
        init(i, M);
    }
    dp[0] = 0;
    int pre;
    for (int mk = 1; mk < bmk; ++mk) {
        dp[mk] = INF;
        for (int i = 0; i < k; ++i) {
            if (mk & (1 << i)) {
                pre = dp[mk ^ (1 << i)];
                if (pre + M > n or pos[i][pre] + M > n) continue;
                dp[mk] = min(dp[mk], pos[i][pre] + M);
            }
        }
        // db(mk) db(dp[mk]) el;
    }
    return dp[bmk - 1] <= n;
}

int main() {
    ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
    cin >> n >> k >> s;
    bmk = 1 << k;
    int L = 0, R = n / k, M;
    // db(L) db(R) db(M) el;
    while (L < R) {
        M = (L + R + 1) / 2;
        if (ok(M)) {
            L = M;
        } else {
            R = M - 1;
        }
    }
    cout << L << "\n";
    return 0;
}
