#include <bits/stdc++.h>
using namespace std;
namespace star {
const int maxn = 1e5 + 10;
int n, k;
int lst = 1, tot = 1, nxt[maxn << 1][26], link[maxn << 1], len[maxn << 1];
inline void insert(int c) {
  if (nxt[lst][c]) {
    int u = lst, y = nxt[lst][c];
    if (len[u] + 1 == len[y])
      lst = y;
    else {
      int cl = ++tot;
      len[cl] = len[u] + 1, link[cl] = link[y],
      memcpy(nxt[cl], nxt[y], sizeof nxt[cl]);
      link[y] = cl;
      while (u and nxt[u][c] == y) nxt[u][c] = cl, u = link[u];
      lst = cl;
    }
    return;
  }
  int x = ++tot;
  len[x] = len[lst] + 1;
  int u = lst;
  while (u and !nxt[u][c]) nxt[u][c] = x, u = link[u];
  if (!u)
    link[x] = 1;
  else {
    int y = nxt[u][c];
    if (len[u] + 1 == len[y])
      link[x] = y;
    else {
      int cl = ++tot;
      len[cl] = len[u] + 1, link[cl] = link[y],
      memcpy(nxt[cl], nxt[y], sizeof nxt[cl]);
      link[x] = link[y] = cl;
      while (u and nxt[u][c] == y) nxt[u][c] = cl, u = link[u];
    }
  }
  lst = x;
}
char s[maxn];
vector<int> pos[maxn];
int tim[maxn << 1], sum[maxn << 1], fa[maxn << 1][20];
inline void work() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1), lst = 1;
    for (char *c = s + 1; *c; c++) insert(*c - 'a'), pos[i].push_back(lst);
  }
  for (int i = 2; i <= tot; i++) fa[i][0] = link[i];
  for (int j = 0; j < 18; j++)
    for (int i = 1; i <= tot; i++) fa[i][j + 1] = fa[fa[i][j]][j];
  for (int i = 1; i <= n; i++)
    for (auto x : pos[i])
      for (; tim[x] != i and x; x = link[x]) tim[x] = i, sum[x]++;
  for (int i = 1; i <= n; i++) {
    long long ans = 0;
    for (auto x : pos[i]) {
      for (int j = 18; ~j; j--)
        if (fa[x][j] and sum[fa[x][j]] < k) x = fa[x][j];
      if (sum[x] < k) x = link[x];
      ans += len[x];
    }
    printf("%lld ", ans);
  }
}
}  // namespace star
signed main() {
  star::work();
  return 0;
}
