#include <bits/stdc++.h>
using namespace std;
struct Tree {
  long long l, r, x;
} t[200010 << 2];
char ch[200010];
long long n, nn, m, k, sa[200010], x[200010], y[200010], buckets[200010],
    rk[200010], height[200010], s[200010], ans[200010], to[200010], vis[200010],
    maxn[200010];
void build(long long l, long long r, long long k) {
  t[k].l = l, t[k].r = r;
  if (l == r) {
    t[k].x = height[l];
    return;
  }
  long long mid = (l + r) >> 1;
  build(l, mid, k << 1);
  build(mid + 1, r, k << 1 | 1);
  t[k].x = min(t[k << 1].x, t[k << 1 | 1].x);
}
long long ask(long long k, long long x, long long y) {
  if (x <= t[k].l && t[k].r <= y) return t[k].x;
  long long mid = (t[k].l + t[k].r) >> 1, ans = 0x7fffffffffffffff;
  if (x <= mid) ans = min(ans, ask(k << 1, x, y));
  if (y > mid) ans = min(ans, ask(k << 1 | 1, x, y));
  return ans;
}
void Resort() {
  for (long long i = 1; i <= m; i++) buckets[i] = 0;
  for (long long i = 1; i <= n; i++) buckets[x[i]]++;
  for (long long i = 2; i <= m; i++) buckets[i] += buckets[i - 1];
  for (long long i = n; i >= 1; i--) sa[buckets[x[y[i]]]--] = y[i];
}
void Suffix_sort() {
  m = nn + 122;
  for (long long i = 1; i <= n; i++) x[i] = s[i], y[i] = i;
  Resort();
  for (long long w = 1; w <= n; w <<= 1) {
    long long tot = 0;
    for (long long i = n - w + 1; i <= n; i++) y[++tot] = i;
    for (long long i = 1; i <= n; i++)
      if (sa[i] > w) y[++tot] = sa[i] - w;
    Resort();
    swap(x, y);
    x[sa[1]] = 1, tot = 1;
    for (long long i = 2; i <= n; i++)
      x[sa[i]] = ((y[sa[i]] == y[sa[i - 1]] && y[sa[i] + w] == y[sa[i - 1] + w])
                      ? tot
                      : ++tot);
    if (tot == n) break;
    m = tot;
  }
}
void get_height() {
  long long k = 0;
  for (long long i = 1; i <= n; i++) rk[sa[i]] = i;
  for (long long i = 1; i <= n; i++) {
    if (rk[i] == 1) continue;
    if (k) k--;
    long long j = sa[rk[i] - 1];
    while (i + k <= n && j + k <= n && s[i + k] == s[j + k]) k++;
    height[rk[i]] = k;
  }
}
int main() {
  scanf("%lld%lld", &nn, &k);
  if (k == 1) {
    for (long long i = 1; i <= nn; i++) {
      scanf("%s", ch + 1);
      long long len = strlen(ch + 1);
      printf("%lld ", (len * (len + 1)) >> 1);
    }
    return 0;
  }
  for (long long i = 1; i <= nn; i++) {
    scanf("%s", ch + 1);
    long long len = strlen(ch + 1);
    for (long long j = 1; j <= len; j++) s[++n] = ch[j] + nn, to[n] = i;
    s[++n] = i;
  }
  Suffix_sort();
  get_height();
  build(1, n, 1);
  long long cnt = 0, now = nn;
  for (long long i = nn + 1; i < n; i++) {
    while (now < n) {
      if (cnt >= k) break;
      now++;
      if (!vis[to[sa[now]]]) cnt++;
      vis[to[sa[now]]]++;
    }
    if (cnt < k) break;
    long long t = ask(1, i + 1, now);
    for (long long j = i; j <= now; j++) maxn[j] = max(maxn[j], t);
    vis[to[sa[i]]]--;
    if (!vis[to[sa[i]]]) cnt--;
  }
  for (long long i = nn + 2; i <= n; i++)
    maxn[i] = max(maxn[i], min(maxn[i - 1], height[i]));
  for (long long i = nn + 1; i <= n; i++) ans[to[sa[i]]] += maxn[i];
  for (long long i = 1; i <= nn; i++) printf("%lld ", ans[i]);
  return 0;
}
