#include <bits/stdc++.h>
using namespace std;
const int maxn = 400010;
int N, K, L, times;
string s[maxn];
struct SAM {
  int np, p, nq, q, Last, cnt, cur[maxn], sum[maxn];
  int maxlen[maxn], fa[maxn], ch[maxn][26];
  SAM() { Last = cnt = 1; }
  void add(int x) {
    np = ++cnt;
    p = Last;
    Last = np;
    maxlen[np] = maxlen[p] + 1;
    while (p && !ch[p][x]) ch[p][x] = np, p = fa[p];
    if (!p)
      fa[np] = 1;
    else {
      q = ch[p][x];
      if (maxlen[q] == maxlen[p] + 1)
        fa[np] = q;
      else {
        nq = ++cnt;
        memcpy(ch[nq], ch[q], sizeof(ch[nq]));
        cur[nq] = cur[q];
        sum[nq] = sum[q];
        fa[nq] = fa[q];
        fa[np] = fa[q] = nq;
        maxlen[nq] = maxlen[p] + 1;
        while (p && ch[p][x] == q) ch[p][x] = nq, p = fa[p];
      }
    }
    while (np) {
      if (cur[np] == times || sum[np] >= K) break;
      sum[np]++;
      cur[np] = times;
      np = fa[np];
    }
  }
  void sort() {}
  void solve(int now) {
    int tp = 1;
    long long ans = 0;
    L = s[now].length();
    for (int i = 0; i < L; i++) {
      tp = ch[tp][s[now][i] - 'a'];
      int kkk = tp;
      while (kkk > 1) {
        if (sum[kkk] >= K) {
          ans += maxlen[kkk];
          break;
        }
        kkk = fa[kkk];
      }
    }
    printf("%lld ", ans);
  }
} sam;
int main() {
  scanf("%d%d", &N, &K);
  for (int i = 1; i <= N; i++) {
    cin >> s[i];
    L = s[i].length();
    sam.Last = 1;
    times = i;
    for (int j = 0; j < L; j++) sam.add(s[i][j] - 'a');
  }
  if (K > N) {
    for (int i = 1; i <= N; i++) printf("0 ");
    return 0;
  }
  sam.sort();
  for (int i = 1; i <= N; i++) sam.solve(i);
  return 0;
}
