#include <bits/stdc++.h>
using namespace std;
const int N = 222222;
const int M = N * 2;
int get_key(char ch) { return ch == '$' ? 26 : ch - 'a'; }
string strings[N];
vector<int> e[M];
int sum[M], top[M];
int n, m;
struct suffix_automaton {
  int son[M][27], fail[M], step[M], cover[M], last, total, root;
  void clear(int u) {
    memset(son[u], 0, sizeof(son[u]));
    fail[u] = step[u] = cover[u] = 0;
  }
  void copy_data(int to, int from) {
    memcpy(son[to], son[from], sizeof(son[from]));
    fail[to] = fail[from];
    step[to] = step[from];
    cover[to] = cover[from];
  }
  void init() {
    root = total = last = 1;
    clear(1);
  }
  void Extend(char ch, int pos) {
    int key = get_key(ch);
    int u = ++total, node;
    clear(u);
    step[u] = step[last] + 1;
    cover[u] = pos;
    for (node = last; node && son[node][key] == 0; node = fail[node])
      son[node][key] = u;
    if (node == 0)
      fail[u] = 1;
    else {
      int v = son[node][key];
      if (step[v] == step[node] + 1)
        fail[u] = v;
      else {
        int nv = ++total;
        copy_data(nv, v);
        step[nv] = step[node] + 1;
        fail[u] = fail[v] = nv;
        for (; node && son[node][key] == v; node = fail[node])
          son[node][key] = nv;
      }
    }
    last = u;
  }
  long long solve(int k) {
    long long ret = 0;
    for (int i = 0, len = 0, u = 1; i < strings[k].size(); ++i) {
      int key = get_key(strings[k][i]);
      if (son[u][key]) {
        u = son[u][key];
        len++;
      } else {
        while (u && son[u][key] == 0) u = fail[u];
        if (u == 0) {
          u = root;
          len = 0;
        } else {
          len = step[u] + 1;
          u = son[u][key];
        }
      }
      int p = top[u];
      if (p > 1) {
        if (p == u)
          ret += len;
        else
          ret += step[p];
      }
    }
    return ret;
  }
} suf;
set<int>* merge(set<int>* a, set<int>* b) {
  if (a->size() > b->size()) swap(a, b);
  b->insert((*a).begin(), (*a).end());
  delete a;
  return b;
}
set<int>* dfs1(int u) {
  set<int>* flag = new set<int>();
  if (suf.cover[u] != -1) flag->insert(suf.cover[u]);
  for (int i = 0; i < e[u].size(); ++i) {
    set<int>* nxt = dfs1(e[u][i]);
    flag = merge(flag, nxt);
  }
  sum[u] = flag->size();
  return flag;
}
void dfs2(int u, int now) {
  if (sum[u] >= m) now = u;
  top[u] = now;
  for (int i = 0; i < e[u].size(); ++i) dfs2(e[u][i], now);
}
int main() {
  suf.init();
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> strings[i];
    for (int j = 0; j < strings[i].size(); ++j) suf.Extend(strings[i][j], i);
    suf.Extend('$', -1);
  }
  for (int i = suf.total; i >= 1; --i) e[suf.fail[i]].push_back(i);
  dfs1(1);
  dfs2(1, 0);
  for (int i = 1; i <= n; ++i) cout << suf.solve(i) << ' ';
  cout << endl;
}
