#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
string s[maxn];
const int kind = 27;
int n, k;
struct state {
  state *Next[kind], *link;
  int len;
  int idx;
  int pref;
  state() {
    idx = 0;
    link = 0;
    len = 0;
    pref = 0;
    memset(Next, 0, sizeof Next);
  }
};
class sam {
 public:
  int sz;
  state st[maxn * 2];
  int f[maxn * 2];
  vector<int> G[maxn * 2];
  long long cnt[maxn * 2];
  inline state* newnode(int len = 0) {
    memset(st[sz].Next, 0, sizeof(st[sz].Next));
    st[sz].link = 0;
    st[sz].len = len;
    st[sz].idx = sz;
    return &st[sz++];
  }
  state *root, *last;
  void extend(int w, int id) {
    state* p = last;
    state* cur = newnode(p->len + 1);
    cur->pref = id;
    while (p && p->Next[w] == 0) {
      p->Next[w] = cur;
      p = p->link;
    }
    if (p) {
      state* q = p->Next[w];
      if (p->len + 1 == q->len)
        cur->link = q;
      else {
        state* clone = newnode(p->len + 1);
        clone->pref = q->pref;
        memcpy(clone->Next, q->Next, sizeof(q->Next));
        clone->link = q->link;
        q->link = clone;
        cur->link = clone;
        while (p && p->Next[w] == q) {
          p->Next[w] = clone;
          p = p->link;
        }
      }
    } else
      cur->link = root;
    last = cur;
  }
  void init() {
    sz = 0;
    root = newnode();
    last = root;
  }
  set<int> ss[maxn * 2];
  void comb(int u, int v) {
    if (ss[u].size() < ss[v].size()) swap(ss[u], ss[v]);
    for (auto iter = ss[v].begin(); iter != ss[v].end(); iter++)
      ss[u].insert(*iter);
    ss[v].clear();
  }
  void dfs(int u, int fa) {
    ss[u].insert(st[u].pref);
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      dfs(v, u);
      comb(u, v);
    }
    cnt[u] = ss[u].size();
  }
  void dfs2(int u, int t) {
    f[u] = t;
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      int tp = t;
      if (cnt[v] >= k) tp = v;
      dfs2(v, tp);
    }
  }
  void calc() {
    for (int i = 1; i < sz; i++) G[st[i].link->idx].push_back(i);
    dfs(0, 0);
    dfs2(0, 0);
  }
  vector<long long> solve() {
    vector<long long> ret;
    for (int i = 1; i <= n; i++) {
      state* cur = root;
      long long ans = 0;
      for (int j = 0; j < s[i].size(); j++) {
        cur = cur->Next[s[i][j] - 'a'];
        ans += min(st[f[cur->idx]].len, j + 1);
      }
      ret.push_back(ans);
    }
    return ret;
  }
} A;
int main() {
  ios::sync_with_stdio(false);
  A.init();
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    for (int j = 0; j < s[i].size(); j++) A.extend(s[i][j] - 'a', i);
    A.extend(26, i);
  }
  A.calc();
  vector<long long> ans = A.solve();
  for (int i = 0; i < ans.size(); i++) cout << ans[i] << ' ';
  cout << '\n';
}
