#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int sa[N], t1[N], t2[N], c[N];
int Rank[N], height[N];
void buildsa(int *s, int n, int m) {
  int i, k, p, a1, a2, *x = t1, *y = t2;
  memset(c, 0, m << 2);
  for (i = 0; i < n; ++i) ++c[x[i] = s[i]];
  for (i = 1; i < m; ++i) c[i] += c[i - 1];
  for (i = n - 1; i >= 0; --i) sa[--c[x[i]]] = i;
  for (k = 1; k < n; k <<= 1) {
    p = 0;
    for (i = n - k; i < n; ++i) y[p++] = i;
    for (i = 0; i < n; ++i)
      if (sa[i] >= k) y[p++] = sa[i] - k;
    memset(c, 0, m << 2);
    for (i = 0; i < n; ++i) ++c[x[y[i]]];
    for (i = 1; i < m; ++i) c[i] += c[i - 1];
    for (i = n - 1; i >= 0; --i) sa[--c[x[y[i]]]] = y[i];
    swap(x, y), p = 1, x[sa[0]] = 0;
    for (i = 1; i < n; ++i) {
      a1 = sa[i - 1] + k < n ? y[sa[i - 1] + k] : -1;
      a2 = sa[i] + k < n ? y[sa[i] + k] : -1;
      x[sa[i]] = (y[sa[i - 1]] == y[sa[i]] && a1 == a2) ? p - 1 : p++;
    }
    if (p >= n) break;
    m = p;
  }
  for (i = 0; i < n; ++i) Rank[sa[i]] = i;
  for (i = 0, k = 0; i < n; ++i) {
    if (k) --k;
    if (!Rank[i]) continue;
    int j = sa[Rank[i] - 1];
    while (s[i + k] == s[j + k]) ++k;
    height[Rank[i]] = k;
  }
}
char s[N];
int n, K, m, a[N], pos[N], bel[N], cnt[N], R[N];
int f[18][N], LOG[N];
long long res[N];
int RMQ(int l, int r) {
  int j = LOG[r - l + 1];
  return min(f[j][l], f[j][r - (1 << j) + 1]);
}
bool check(int p, int len) {
  int l, r, mid;
  l = m, r = p;
  while (l < r) {
    mid = l + r >> 1;
    if (RMQ(mid + 1, p) >= len)
      r = mid;
    else
      l = mid + 1;
  }
  int Left = l;
  l = p, r = n - 1;
  while (l < r) {
    mid = l + r + 1 >> 1;
    if (RMQ(p + 1, mid) >= len)
      l = mid;
    else
      r = mid - 1;
  }
  int Right = l;
  return R[Left] <= Right;
}
void work() {
  int i, j, k, x;
  scanf("%d%d", &m, &K);
  for (i = 0; i < m; ++i) {
    scanf("%s", s);
    for (j = 0; s[j]; ++j) a[n++] = s[j] - 'a' + m;
    pos[i] = n;
    a[n++] = i;
  }
  buildsa(a, n, m + 26);
  for (i = m; i < n; ++i) bel[i] = lower_bound(pos, pos + m, sa[i]) - pos;
  for (i = m, j = m, k = 0; i < n; ++i) {
    while (j < n && k < K) {
      if ((cnt[bel[j]])++ == 0) ++k;
      ++j;
    }
    R[i] = k < K ? n : j - 1;
    if ((--cnt[bel[i]]) == 0) --k;
  }
  for (i = 0; 1 << i <= n; ++i) LOG[1 << i] = i;
  for (i = 2; i <= n; ++i)
    if (!LOG[i]) LOG[i] = LOG[i - 1];
  for (i = 0; i < n; ++i) f[0][i] = height[i];
  for (j = 1; 1 << j <= n; ++j)
    for (i = 0; i + (1 << j) <= n; ++i)
      f[j][i] = min(f[j - 1][i], f[j - 1][i + (1 << j - 1)]);
  for (i = m; i < n; ++i) {
    int l = 0, r = pos[bel[i]] - sa[i], mid;
    while (l < r) {
      mid = l + r + 1 >> 1;
      if (check(i, mid))
        l = mid;
      else
        r = mid - 1;
    }
    res[bel[i]] += l;
  }
  for (i = 0; i < m; ++i) printf("%lld\n", res[i]);
}
int main() {
  work();
  return 0;
}
