#include <bits/stdc++.h>
using namespace std;
int N, M, K;
int NN = 1e5;
int sa[200005], rk[200005], cnt[200005], oldrk[200005 * 2], id[200005],
    px[200005];
int a[200005];
int h[200005], st[200005][21];
inline bool cmp(int x, int y, int k) {
  return oldrk[x] == oldrk[y] && oldrk[x + k] == oldrk[y + k];
}
void SA() {
  int i, p = 0, k, m = 2 * NN;
  memset(cnt, 0, sizeof(cnt));
  for (i = 1; i <= N; ++i) ++cnt[rk[i] = a[i]];
  for (i = 1; i <= m; ++i) cnt[i] += cnt[i - 1];
  for (i = N; i >= 1; --i) sa[cnt[rk[i]]--] = i;
  for (k = 1; k < N; k <<= 1, m = p) {
    for (p = 0, i = N; i > N - k; --i) id[++p] = i;
    for (i = 1; i <= N; ++i) {
      if (sa[i] > k) id[++p] = sa[i] - k;
    }
    for (int i = 0; i <= m; i++) cnt[i] = 0;
    for (i = 1; i <= N; ++i) ++cnt[px[i] = rk[id[i]]];
    for (i = 1; i <= m; ++i) cnt[i] += cnt[i - 1];
    for (i = N; i >= 1; --i) sa[cnt[px[i]]--] = id[i];
    memcpy(oldrk, rk, sizeof(rk));
    for (p = 0, i = 1; i <= N; ++i) {
      rk[sa[i]] = cmp(sa[i], sa[i - 1], k) ? p : ++p;
    }
  }
  for (i = 1; i <= N; i++) {
    if (rk[i] == 1) continue;
    int k = max(h[rk[i - 1]] - 1, 0);
    while (i + k <= N && a[i + k] == a[sa[rk[i] - 1] + k]) k++;
    h[rk[i]] = k;
  }
  for (i = 1; i <= N; i++) st[i][0] = h[i];
  for (int j = 1; (1 << j) < N; j++) {
    for (i = 1; i + (1 << j) - 1 <= N; i++)
      st[i][j] = min(st[i][j - 1], st[i + (1 << j - 1)][j - 1]);
  }
}
int len[200005];
long long lcp(int l, int r) {
  if (l == r) return len[sa[l]];
  if (l > r) swap(l, r);
  ++l;
  int k = log2(r - l + 1);
  return min(st[l][k], st[r - (1 << k) + 1][k]);
}
int ID[200005], m = 0;
void insert(int i) {
  ++cnt[ID[sa[i]]];
  if (ID[sa[i]] && cnt[ID[sa[i]]] == 1) ++m;
}
void remove(int i) {
  --cnt[ID[sa[i]]];
  if (ID[sa[i]] && cnt[ID[sa[i]]] == 0) --m;
}
long long ans[200005];
int res[200005];
vector<int> adj[200005];
int main() {
  cin >> M >> K;
  string s;
  for (int i = 1; i <= M; i++) {
    cin >> s;
    a[++N] = NN + i;
    for (int j = 0; j < s.length(); j++) {
      a[++N] = s[j];
      ID[N] = i;
      len[N] = s.length() - j;
    }
  }
  SA();
  for (int i = 1; i <= N; i++) {
  }
  memset(cnt, 0, sizeof(cnt));
  int r = 0;
  multiset<int> st;
  multiset<int>::iterator it;
  for (int l = 1; l <= N; l++) {
    while (r < l || r <= N && m < K) insert(++r);
    if (ID[sa[l]] && m == K) {
      st.insert(lcp(l, r));
      adj[r].push_back(lcp(l, r));
    }
    if (!st.empty()) {
      it = st.end();
      --it;
      res[l] = (*it);
    }
    remove(l);
    for (int k = 0; k < adj[l].size(); k++) {
      int x = adj[l][k];
      st.erase(st.find(x));
    }
  }
  st.clear();
  for (int i = 1; i <= N; i++) adj[i].clear();
  int l = N + 1;
  for (int r = N; r >= 1; r--) {
    while (l > r || l > 1 && m < K) insert(--l);
    if (ID[sa[r]] && m == K) {
      st.insert(lcp(l, r));
      adj[l].push_back(lcp(l, r));
    }
    if (!st.empty()) {
      it = st.end();
      --it;
      res[r] = max(res[r], (*it));
    }
    remove(r);
    for (int k = 0; k < adj[r].size(); k++) {
      int x = adj[r][k];
      st.erase(st.find(x));
    }
  }
  for (int i = 1; i <= N; i++) {
    ans[ID[sa[i]]] += res[i];
  }
  for (int i = 1; i <= M; i++) {
    cout << ans[i] << " ";
  }
  cout << endl;
  return 0;
}
