#include <bits/stdc++.h>
using namespace std;
const int mxn = (1 << (20));
const int mxl = 20;
int n, stp, mv, suffix[mxn], tmp[mxn];
int sum[mxn], cnt[mxn], rnk[mxl][mxn];
char str[mxn];
int ara[mxn];
inline bool eql(const int &u, const int &v) {
  if (!stp) return (ara[u] == ara[v]);
  if (rnk[stp - 1][u] != rnk[stp - 1][v]) return false;
  int a = u + mv < n ? rnk[stp - 1][u + mv] : -1;
  int b = v + mv < n ? rnk[stp - 1][v + mv] : -1;
  return a == b;
}
void update() {
  for (int i = 0; i < n; i++) sum[i] = 0;
  for (int i = 0, r = 0; i < n; i++) {
    suffix[i] = tmp[i];
    if (i && !eql(suffix[i], suffix[i - 1])) {
      rnk[stp][suffix[i]] = ++r;
      sum[r + 1] = sum[r];
    } else
      rnk[stp][suffix[i]] = r;
    sum[r + 1]++;
  }
}
void Sort() {
  for (int i = 0; i < n; i++) cnt[i] = 0;
  memset(tmp, -1, sizeof tmp);
  for (int i = 0; i < mv; i++) {
    int idx = rnk[stp - 1][n - i - 1];
    int x = sum[idx];
    tmp[x + cnt[idx]] = n - i - 1;
    cnt[idx]++;
  }
  for (int i = 0; i < n; i++) {
    int idx = suffix[i] - mv;
    if (idx < 0) continue;
    idx = rnk[stp - 1][idx];
    int x = sum[idx];
    tmp[x + cnt[idx]] = suffix[i] - mv;
    cnt[idx]++;
  }
  update();
  return;
}
inline bool cmp(const int &a, const int &b) {
  if (ara[a] != ara[b]) return ara[a] < ara[b];
  return false;
}
void SortSuffix() {
  for (int i = 0; i < n; i++) tmp[i] = i;
  sort(tmp, tmp + n, cmp);
  stp = 0;
  update();
  ++stp;
  for (mv = 1; mv < n; mv <<= 1) Sort(), stp++;
  stp--;
  for (int i = 0; i <= stp; i++) rnk[i][n] = -1;
}
inline int lcp(int u, int v) {
  if (u == v) return n - u;
  int ret = 0;
  for (int i = stp; i >= 0; i--)
    if (rnk[i][u] == rnk[i][v]) {
      ret += 1 << i;
      u += 1 << i;
      v += 1 << i;
    }
  return ret;
}
int ind[mxn];
long long res[mxn];
int freq[mxn];
int seg[4 * mxn];
void update(int N, int s, int e, int l, int r, int val) {
  if (s >= l && e <= r) {
    seg[N] = max(seg[N], val);
    return;
  }
  if (s > r || e < l) return;
  int mid = (s + e) >> 1;
  update(N * 2, s, mid, l, r, val);
  update(N * 2 + 1, mid + 1, e, l, r, val);
  return;
}
int query(int N, int s, int e, int indx, int c) {
  if (s == indx && e == indx) return max(seg[N], c);
  if (s > indx || e < indx) return 0;
  int mid = (s + e) >> 1;
  return max(query(N * 2, s, mid, indx, max(c, seg[N])),
             query(N * 2 + 1, mid + 1, e, indx, max(c, seg[N])));
}
int main() {
  int m, k, cnt = 30;
  scanf("%d %d", &m, &k);
  n = 0;
  for (int i = 0; i < m; i++) {
    if (i) ara[n] = ++cnt, ind[n++] = -1;
    scanf("%s", str);
    if (k == 1) res[i] = (1LL * (strlen(str)) * (strlen(str) + 1)) >> 1;
    for (int j = 0; str[j]; j++) ara[n] = (str[j] - 'a' + 1), ind[n++] = i;
  }
  if (k == 1) {
    for (int i = 0; i < m; i++) printf("%lld ", res[i]);
    return 0;
  }
  SortSuffix();
  int l = 0, f = 0, lst = -1;
  for (int i = 0; i < n && ind[suffix[i]] != -1; i++) {
    freq[ind[suffix[i]]]++;
    if (freq[ind[suffix[i]]] == 1) f++;
    if (f >= k) {
      while (f >= k) {
        update(1, 1, n + 1, l + 1, i + 1, lcp(suffix[l], suffix[i]));
        freq[ind[suffix[l]]]--;
        if (!freq[ind[suffix[l]]]) f--;
        l++;
      }
      f++, l--, freq[ind[suffix[l]]]++;
    }
  }
  for (int i = 0; i < n; i++)
    if (ind[suffix[i]] != -1)
      res[ind[suffix[i]]] += query(1, 1, n + 1, i + 1, 0);
  for (int i = 0; i < m; i++) printf("%lld ", res[i]);
  puts("");
  return 0;
}
