#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
struct SAMunit {
  int len;
  int ch[26], fa;
} t[N << 1];
int tot = 1;
void extend(int& las, int c) {
  int p = las, np = las = ++tot;
  t[np].len = t[p].len + 1;
  for (; p && !t[p].ch[c]; p = t[p].fa) t[p].ch[c] = np;
  if (!p)
    t[np].fa = 1;
  else {
    int q = t[p].ch[c];
    if (t[q].len == t[p].len + 1)
      t[np].fa = q;
    else {
      int nq = ++tot;
      t[nq] = t[q];
      t[nq].len = t[p].len + 1;
      t[np].fa = t[q].fa = nq;
      for (; p && t[p].ch[c] == q; p = t[p].fa) t[p].ch[c] = nq;
    }
  }
}
set<int> ids[N];
int ch[N][26], pos[N];
int pool;
void insert(char a[], int id) {
  pos[0] = 1;
  for (int i = 0, now = 0; a[i]; i++) {
    int c = a[i] - 'a';
    int& son = ch[now][a[i] - 'a'];
    if (!son) extend(pos[son = ++pool] = pos[now], c);
    now = son;
    ids[pos[now]].insert(id);
  }
}
char a[N];
int n, m;
vector<char> str[N];
int one[N], idx;
int ver[N], Next[N];
inline void AddEdge(int a, int b) {
  Next[idx] = one[a];
  ver[idx] = b;
  one[a] = idx++;
}
int b[N];
void dfs(int x) {
  for (int i = one[x], y; ~i; i = Next[i]) {
    dfs(y = ver[i]);
    if (ids[y].size() > ids[x].size()) swap(ids[x], ids[y]);
    for (set<int>::iterator it = ids[y].begin(); it != ids[y].end(); it++)
      ids[x].insert(*it);
    ids[y].clear();
  }
  if ((int)ids[x].size() >= m) b[x] = t[x].len - t[t[x].fa].len;
}
void dp(int x) {
  b[x] += b[t[x].fa];
  for (int i = one[x]; ~i; i = Next[i]) dp(ver[i]);
}
int vis[N];
int main() {
  int i, j;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    scanf("%s", a);
    insert(a, i);
    for (j = 0; a[j]; j++) str[i].push_back(a[j]);
  }
  memset(one, -1, sizeof one);
  for (i = 2; i <= tot; i++) AddEdge(t[i].fa, i);
  dfs(1);
  dp(1);
  for (i = 1; i <= n; i++) {
    int cur = 0;
    long long ans = 0;
    for (j = 0; j < (int)str[i].size(); j++) {
      cur = ch[cur][str[i][j] - 'a'];
      ans += b[pos[cur]];
    }
    printf("%lld ", ans);
  }
  return 0;
}
