#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, LG = 18;
int lcp[N][LG], lg[N], dp[N], en[N];
int n, k, m, id[N], rnk[N], rev[N];
pair<pair<int, int>, int> x[N];
vector<int> rem[N];
long long ans[N];
char c[N];
inline void set_rank() {
  sort(x, x + m);
  for (int i = 1, cur = 0; i < m; i++)
    rnk[x[i].second] = cur += x[i].first != x[i - 1].first;
}
inline void make_suffix_array() {
  for (int i = 0; i < m; i++) x[i] = {{c[i], 0}, i};
  set_rank();
  for (int i = 0; m >> i; i++) {
    for (int j = 0; j < m; j++)
      x[j] = {{rnk[j], j + (1 << i) < en[id[j]] ? rnk[j + (1 << i)] : -1}, j};
    set_rank();
  }
  for (int i = 0; i < m; i++) rev[rnk[i]]++;
  partial_sum(rev, rev + m, rev);
  for (int i = m - 1; ~i; i--) rnk[i] = --rev[rnk[i]];
}
inline void make_lcp_array() {
  rev[m] = m;
  for (int i = 0; i < m; i++) rev[rnk[i]] = i;
  for (int i = 0, cur = 0; i < m; i++) {
    cur = max(cur, 0);
    int limit = min(en[id[i]] - i, en[id[rev[rnk[i] + 1]]] - rev[rnk[i] + 1]);
    while (c[i + cur] == c[rev[rnk[i] + 1] + cur] && cur < limit) cur++;
    lcp[rnk[i]][0] = min(cur--, en[id[i]] - i);
  }
  for (int i = m - 1; ~i; i--)
    for (int j = 0; j < LG - 1; j++)
      lcp[i][j + 1] =
          min(lcp[i][j], i + (1 << j) < m ? lcp[i + (1 << j)][j] : m);
  for (int i = 2; i <= m; i++) lg[i] = lg[i / 2] + 1;
}
inline int get_lcp(int i, int j) {
  return i < j ? min(lcp[i][lg[j - i]], lcp[j - (1 << lg[j - i])][lg[j - i]])
               : en[id[rev[i]]] - rev[i];
}
inline void find_min_segments() {
  int p = m - 1, v = 0, cnt[N] = {};
  for (int i = m - 1; ~i; i--) {
    v += !cnt[id[rev[i]]]++;
    while (v > k || cnt[id[rev[p]]] > 1) v -= !--cnt[id[rev[p--]]];
    dp[i] = v < k ? -1 : p;
    if (~dp[i]) rem[p].push_back(i);
  }
}
inline void calculate_nums() {
  multiset<int> s = {0};
  for (int i = 0, prv = -1; i < m; i++) {
    if (~dp[i]) s.insert(get_lcp(i, dp[i]));
    for (auto p : rem[i]) {
      s.erase(s.find(get_lcp(p, i)));
      prv = max(prv, p);
    }
    ans[id[rev[i]]] += max(*s.rbegin(), ~prv ? get_lcp(prv, i) : 0);
  }
}
inline void read_input() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%s", c + m);
    fill(id + m, id + strlen(c), i);
    m = en[i] = strlen(c);
  }
}
inline void solve() {
  make_suffix_array();
  make_lcp_array();
  find_min_segments();
  calculate_nums();
}
inline void write_output() {
  for (int i = 0; i < n; i++) printf("%lld ", ans[i]);
}
int main() {
  read_input();
  solve();
  write_output();
}
