#include <bits/stdc++.h>
using namespace std;
int n, t;
int p[1000005], r[1000005], h[1000005];
string s;
void fix_index(int *b, int *e) {
  int pkm1, pk, np, i, d, m;
  pkm1 = p[*b + t];
  m = e - b;
  d = 0;
  np = b - r;
  for (i = 0; i < m; i++) {
    if (((pk = p[*b + t]) != pkm1) && !(np <= pkm1 && pk < np + m)) {
      pkm1 = pk;
      d = i;
    }
    p[*(b++)] = np + d;
  }
}
bool comp(int i, int j) { return p[i + t] < p[j + t]; }
void suff_arr() {
  int i, j, bc[256];
  t = 1;
  for (i = 0; i < 256; i++) bc[i] = 0;
  for (i = 0; i < n; i++) ++bc[int(s[i])];
  for (i = 1; i < 256; i++) bc[i] += bc[i - 1];
  for (i = 0; i < n; i++) r[--bc[int(s[i])]] = i;
  for (i = n - 1; i >= 0; i--) p[i] = bc[int(s[i])];
  for (t = 1; t < n; t *= 2) {
    for (i = 0, j = 1; i < n; i = j++) {
      while (j < n && p[r[j]] == p[r[i]]) ++j;
      if (j - i > 1) {
        sort(r + i, r + j, comp);
        fix_index(r + i, r + j);
      }
    }
  }
}
void lcp() {
  int tam = 0, i, j;
  for (i = 0; i < n; i++)
    if (p[i] > 0) {
      j = r[p[i] - 1];
      while (s[i + tam] == s[j + tam]) ++tam;
      h[p[i] - 1] = tam;
      if (tam > 0) --tam;
    }
  h[n - 1] = 0;
}
int m, k;
int qual[1000000];
int term[1000000];
struct no {
  int lazy;
  int m, esq, dir;
};
no v[1000000];
void monta(int tam) {
  while (tam & (tam - 1)) tam++;
  for (int i = tam; i < 2 * tam; i++) {
    v[i].lazy = v[i].m = 0;
    v[i].esq = v[i].dir = i - tam;
  }
  for (int i = tam - 1; i >= 1; i--) {
    v[i].lazy = v[i].m = 0;
    v[i].esq = v[2 * i].esq;
    v[i].dir = v[2 * i + 1].dir;
  }
}
void refresh(int i) {
  if (v[i].lazy) {
    v[i].m = max(v[i].m, v[i].lazy);
    if (v[i].esq != v[i].dir) {
      v[2 * i].lazy = max(v[2 * i].lazy, v[i].lazy);
      v[2 * i + 1].lazy = max(v[2 * i + 1].lazy, v[i].lazy);
    }
    v[i].lazy = 0;
  }
}
int get(int i, int ini, int fim) {
  refresh(i);
  if (fim < v[i].esq || ini > v[i].dir) return 0;
  if (v[i].esq >= ini && v[i].dir <= fim) {
    return v[i].m;
  }
  int ret = max(get(2 * i, ini, fim), get(2 * i + 1, ini, fim));
  v[i].m = max(v[2 * i].m, v[2 * i + 1].m);
  return ret;
}
void atualiza(int i, int ini, int fim, int val) {
  refresh(i);
  if (fim < v[i].esq || ini > v[i].dir) return;
  if (v[i].esq >= ini && v[i].dir <= fim) {
    v[i].lazy = max(v[i].lazy, val);
    refresh(i);
    return;
  }
  atualiza(2 * i, ini, fim, val);
  atualiza(2 * i + 1, ini, fim, val);
  v[i].m = max(v[2 * i].m, v[2 * i + 1].m);
}
int main(void) {
  string a;
  cin >> m >> k;
  if (k == 1) {
    long long ans = 0;
    for (int i = 0; i < m; i++) {
      if (i) printf(" ");
      cin >> a;
      long long sz = a.size();
      cout << ((sz) * (sz + 1)) / 2LL;
    }
    printf("\n");
    return 0;
  }
  int topo = 0;
  for (int i = 0; i < m; i++) {
    cin >> a;
    term[i] = topo + a.size();
    for (int j = topo; j < topo + a.size() + 1; j++) qual[j] = i;
    topo += a.size() + 1;
    s += a;
    s += '$';
  }
  n = s.size();
  suff_arr();
  lcp();
  for (int i = m; i < n; i++) {
    h[i] = min(h[i], term[qual[r[i]]] - r[i]);
  }
  map<int, int> mapa;
  multiset<int> ms;
  set<int> S;
  monta(n);
  int ini = m, fim = ini + 1;
  ms.insert(h[m]);
  S.insert(qual[r[m]]);
  S.insert(qual[r[m + 1]]);
  mapa[qual[r[m]]]++;
  mapa[qual[r[m + 1]]]++;
  while (ini < n && fim < n) {
    if (S.size() >= k) {
      atualiza(1, ini, fim, *(ms.begin()));
      if (S.size() == k && mapa[qual[r[ini]]] == 1) {
        if (fim == n - 1) break;
        fim++;
        ms.insert(h[fim - 1]);
        S.insert(qual[r[fim]]);
        mapa[qual[r[fim]]]++;
      } else {
        ms.erase(ms.find(h[ini]));
        mapa[qual[r[ini]]]--;
        if (mapa[qual[r[ini]]] == 0) S.erase(S.find(qual[r[ini]]));
        ini++;
      }
    } else {
      if (fim == n - 1) break;
      fim++;
      ms.insert(h[fim - 1]);
      S.insert(qual[r[fim]]);
      mapa[qual[r[fim]]]++;
    }
  }
  long long ans[1000000];
  memset(ans, 0, sizeof ans);
  for (int i = m; i < n; i++) ans[qual[r[i]]] += get(1, i, i);
  for (int i = 0; i < m; i++) {
    if (i) printf(" ");
    cout << ans[i];
  }
  printf("\n");
  return 0;
}
