#include<cstdio> #include<queue> #include<cstring> usingnamespace std; constint N = 1e4 + 10, M = 1e3 + 10, inf = 2e4; char s[N]; int n, m, f[N][M]; int idx, cnt[N], nxt[N], tr[N][3]; voidchkmax(int &x, int k) { k > x && (x = k); } voidbuild() { queue <int> q; for (int i = 0; i < 3; i++) tr[0][i] && (q.push(tr[0][i]), 0); while (!q.empty()) { int t = q.front(); q.pop(); for (int i = 0; i < 3; i++) { int &p = tr[t][i]; if (!p) p = tr[nxt[t]][i]; else { nxt[p] = tr[nxt[t]][i]; q.push(p); } } cnt[t] += cnt[nxt[t]]; } } voidinsert(int len, char *str) { int p = 0; for (int i = 1; i <= len; i++) { int &t = tr[p][str[i] - 'A']; !t && (t = ++idx); p = t; } cnt[p]++; } voiddfs(int x) { for (int i = 0; i < 3; i++) { if (!tr[x][i]) continue; for (int j = 0; j < m; j++) chkmax(f[tr[x][i]][j + 1], f[x][j] + cnt[tr[x][i]]); dfs(tr[x][i]); } } intmain() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%s", s + 1); insert(strlen(s + 1), s); } build(); for (int i = 1; i <= idx; i++) for (int j = 0; j <= m; j++) f[i][j] = -inf; for (int k = 0; k < m; k++) for (int i = 0; i <= idx; i++) for (int j = 0; j < 3; j++) chkmax(f[tr[i][j]][k + 1], f[i][k] + cnt[tr[i][j]]); int res = 0; for (int i = 0; i <= idx; i++) chkmax(res, f[i][m]); printf("%d", res); return0; }