#include <bits/stdc++.h>
int A[201000], B[201000], a[201000], c[201000], v[201000], sa[201000],
    rk[201000], h[201000], bl[201000], d[201000];
int n, m, K, i, j, len, cnt, f = 1, b, r, q[201000], l[201000], w[201000];
long long ans[201000];
char s[201000];
bool cmp(int *s, int a, int b, int l) {
  return s[a] == s[b] && s[a + l] == s[b + l];
}
void SA(int n) {
  int i, j, k = 0, p, m = n + 255, *x = A, *y = B, *t;
  for (i = 0; i < m; i++) c[i] = 0;
  for (i = 0; i < n; i++) c[x[i] = a[i]]++;
  for (i = 0; i < m; i++) c[i + 1] += c[i];
  for (i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i;
  for (j = 1, p = 1; p < n; j *= 2, m = p) {
    for (p = 0, i = n - j; i < n; i++) y[p++] = i;
    for (i = 0; i < n; i++)
      if (sa[i] >= j) y[p++] = sa[i] - j;
    for (i = 0; i < m; i++) c[i] = 0;
    for (i = 0; i < n; i++) c[v[i] = x[y[i]]]++;
    for (i = 0; i < m; i++) c[i + 1] += c[i];
    for (i = n - 1; i >= 0; i--) sa[--c[v[i]]] = y[i];
    for (t = x, x = y, y = t, i = 1, p = 1, x[sa[0]] = 0; i < n; i++)
      x[sa[i]] = cmp(y, sa[i], sa[i - 1], j) ? p - 1 : p++;
  }
  for (i = 0; i < n; i++) rk[sa[i]] = i;
  for (i = 0; i < n; h[rk[i++]] = k)
    for (k ? k-- : 0, j = sa[rk[i] - 1]; a[i + k] == a[j + k]; k++)
      ;
}
int main() {
  scanf("%d%d", &n, &K);
  for (i = 0; i < n; i++) {
    scanf("%s", s), len = strlen(s);
    for (j = 0; j < len; j++) bl[m] = i + 1, d[m] = len - j, a[m++] = s[j];
    a[m++] = i + 255;
  }
  SA(++m);
  for (i = 0; i < m; i++) c[i] = v[i] = 0;
  c[0] = m;
  for (i = 0; i < m; i++) {
    while (cnt < K && r < m) {
      l[r] = i, cnt += !(c[bl[sa[r]]]++);
      while (f <= b && h[q[b]] > h[r]) b--;
      q[++b] = r;
      w[r++] = h[q[f]];
    }
    if (cnt < K) break;
    f += f <= b && q[f] <= i;
    v[i] = f > b ? d[sa[i]] : h[q[f]];
    cnt -= !(--c[bl[sa[i]]]);
  }
  f = 1, b = 0;
  for (i = 0; i < m; i++) {
    while (f <= b && v[q[b]] < v[i]) b--;
    q[++b] = i;
    while (f <= b && q[f] < l[i]) f++;
    if (f <= b && v[q[f]] > w[i]) w[i] = v[q[f]];
    ans[bl[sa[i]]] += w[i];
  }
  for (i = 0; i < n; i++) printf("%I64d%s", ans[i + 1], i + 1 < n ? " " : "\n");
  return 0;
}
