#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
struct ST {
  int l, r, mx, c;
} t[N << 2];
int n, k, m, nn, tot, R, sum;
int ls[N], rs[N], a[N], rk[N], sa[N], h[N], tp[N], tax[N], f[N][18], lg[N],
    tag[N], len[N], num[N];
long long ans[N];
char s[N];
void rsort() {
  int i;
  for (i = 0; i <= m; i++) tax[i] = 0;
  for (i = 1; i <= n; i++) tax[rk[tp[i]]]++;
  for (i = 1; i <= m; i++) tax[i] += tax[i - 1];
  for (i = n; i >= 1; i--) sa[tax[rk[tp[i]]]--] = tp[i];
}
void suffix() {
  int i;
  for (i = 1; i <= n; i++) rk[i] = a[i], tp[i] = i;
  m = tot;
  rsort();
  for (int w = 1, p = 0; p < n; w += w, m = p) {
    p = 0;
    for (i = n - w + 1; i <= n; i++) tp[++p] = i;
    for (i = 1; i <= n; i++)
      if (sa[i] > w) tp[++p] = sa[i] - w;
    rsort();
    swap(rk, tp);
    rk[sa[1]] = p = 1;
    for (i = 2; i <= n; i++)
      if (tp[sa[i]] == tp[sa[i - 1]] && tp[sa[i] + w] == tp[sa[i - 1] + w])
        rk[sa[i]] = p;
      else
        rk[sa[i]] = ++p;
  }
}
void height() {
  int i, j = 0, k = 0;
  for (i = 1; i <= n; i++) rk[sa[i]] = i;
  for (i = 1; i <= n; i++) {
    if (k) k--;
    if (rk[i] == 1) continue;
    j = sa[rk[i] - 1];
    while (a[j + k] == a[i + k]) k++;
    h[rk[i]] = k;
  }
}
void build(int k, int l, int r) {
  t[k].l = l;
  t[k].r = r;
  t[k].mx = 0;
  t[k].c = 0;
  if (l == r) return;
  int mid = (l + r) >> 1;
  build(k << 1, l, mid);
  build(k << 1 | 1, mid + 1, r);
}
void add(int k, int w) {
  t[k].mx = max(t[k].mx, w);
  t[k].c = max(t[k].c, w);
}
void pushdown(int k) {
  if (t[k].c) {
    add(k << 1, t[k].c);
    add(k << 1 | 1, t[k].c);
    t[k].c = 0;
  }
}
void modify(int k, int L, int R, int w) {
  if (L <= t[k].l && t[k].r <= R) {
    add(k, w);
    return;
  }
  pushdown(k);
  int mid = (t[k].l + t[k].r) >> 1;
  if (L <= mid) modify(k << 1, L, R, w);
  if (R > mid) modify(k << 1 | 1, L, R, w);
}
int query(int k, int p) {
  if (t[k].l == t[k].r) return t[k].mx;
  pushdown(k);
  int mid = (t[k].l + t[k].r) >> 1;
  if (p <= mid)
    return query(k << 1, p);
  else
    return query(k << 1 | 1, p);
}
void init() {
  int i, j;
  for (i = 1; i <= n; i++) f[i][0] = h[i];
  for (i = 1; i <= n; i++) lg[i] = log2(i);
  for (i = 1; i <= 17; i++)
    for (j = 1; j + (1 << i) - 1 <= n; j++)
      f[j][i] = min(f[j][i - 1], f[j + (1 << i - 1)][i - 1]);
}
int rmq(int l, int r) {
  l = rk[l];
  r = rk[r];
  if (l > r) swap(l, r);
  l++;
  if (l > r) return len[sa[r]];
  int d = lg[r - l + 1];
  return min(f[l][d], f[r - (1 << d) + 1][d]);
}
void solve() {
  int i, l1, r1 = 0, l2 = 0, r2 = 0;
  build(1, 1, n);
  for (l1 = 1; l1 <= n; l1++) {
    if (!tag[sa[l1]]) continue;
    if (l1 > 1) {
      num[tag[sa[l1 - 1]]]--;
      if (!num[tag[sa[l1 - 1]]] && tag[sa[l1 - 1]]) sum--;
    }
    while (r1 < n && sum < k) {
      r1++;
      if (!num[tag[sa[r1]]] && tag[sa[r1]]) sum++;
      num[tag[sa[r1]]]++;
    }
    if (sum < k) break;
    modify(1, l1, r1, rmq(sa[l1], sa[r1]));
    if (l2 > 0)
      for (i = r2 + 1; i <= r1; i++) modify(1, i, i, rmq(sa[l2], sa[i]));
    l2 = l1;
    r2 = r1;
  }
  if (r2 < n && l2 > 0)
    for (i = r2 + 1; i <= n; i++) modify(1, i, i, rmq(sa[l2], sa[i]));
}
int main() {
  int i, j;
  scanf("%d%d", &nn, &k);
  tot = 26;
  for (i = 1; i <= nn; i++) {
    scanf("%s", s + 1);
    m = strlen(s + 1);
    ls[i] = n + 1;
    for (j = 1; j <= m; j++) {
      a[++n] = s[j] - 'a' + 1;
      tag[n] = i;
      len[n] = m - j + 1;
    }
    rs[i] = n;
    if (i < nn) a[++n] = ++tot;
    if (k == 1) ans[i] = 1ll * m * (m + 1) / 2;
  }
  if (k == 1) {
    for (i = 1; i <= nn; i++) printf("%I64d ", ans[i]);
    return 0;
  }
  a[0] = -1;
  a[n + 1] = 1000000000;
  suffix();
  height();
  init();
  solve();
  for (i = 1; i <= n; i++)
    if (tag[sa[i]]) ans[tag[sa[i]]] += 1ll * query(1, i);
  for (i = 1; i <= nn; i++) printf("%I64d\n", ans[i]);
  return 0;
}
