#include <bits/stdc++.h>
using namespace std;
int n;
string s;
const int maxN = 500010;
int len;
const static int maxNO = maxN;
int slen[maxNO], start[maxNO];
int nearest[maxN], belong[maxN];
void mapstring(int n, int term_char) {
  n++;
  for (int i = len - 1, tt = len - 1; i >= 0; i--) {
    if (s[i] == term_char) tt = i, n--;
    nearest[i] = tt;
    belong[i] = n;
  }
}
int ra[maxN], tra[maxN], sa[maxN], tmpsa[maxN], cnt[maxN];
void countSort(int k) {
  int maxr = max(300, len);
  memset(cnt, 0, sizeof(cnt[0]) * (maxr + 5));
  for (int i = 0; i < len; i++) cnt[sa[i] + k < len ? ra[sa[i] + k] : 0]++;
  for (int i = 0, t, sum = 0; i < maxr; i++) t = cnt[i], cnt[i] = sum, sum += t;
  for (int i = 0; i < len; i++)
    tmpsa[cnt[sa[i] + k < len ? ra[sa[i] + k] : 0]++] = sa[i];
  for (int i = 0; i < len; i++) sa[i] = tmpsa[i];
}
void buildsa() {
  for (int i = 0; i < len; i++) ra[i] = s[i], sa[i] = i;
  for (int k = 1; k < len; k <<= 1) {
    countSort(k);
    countSort(0);
    int r = 0;
    tra[sa[0]] = 0;
    for (int i = 1; i < len; i++) {
      int t1 = (sa[i] + k < len ? ra[sa[i] + k] : 0);
      int t2 = (sa[i - 1] + k < len ? ra[sa[i - 1] + k] : 0);
      tra[sa[i]] = (ra[sa[i]] == ra[sa[i - 1]] && t1 == t2) ? r : ++r;
    }
    for (int i = 0; i < len; i++) ra[i] = tra[i];
    if (ra[sa[len - 1]] == len - 1) break;
  }
}
int phi[maxN], LCP[maxN];
void computeLCP() {
  phi[sa[0]] = -1;
  for (int i = 1; i < len; i++) phi[sa[i]] = sa[i - 1];
  int L = 0;
  for (int i = 0; i < len; i++) {
    if (phi[i] == -1) {
      LCP[ra[i]] = 0;
      continue;
    }
    while (L < len && s[i + L] == s[phi[i] + L]) L++;
    int TL = min(nearest[i] - i, L);
    LCP[ra[i]] = TL;
    L = max(L - 1, 0);
  }
}
int occ[maxNO], mxv[maxN], ans[maxN];
long long fans[maxN];
void qry(int k) {
  set<pair<int, int> > box, box2;
  memset(occ, 0, sizeof(occ));
  int distinct_cnt = 0;
  for (int i = 0; i < len; i++) {
    distinct_cnt += !occ[belong[sa[i]]];
    occ[belong[sa[i]]]++;
    box.insert(make_pair(LCP[i], i));
    while (distinct_cnt == k) {
      int pos = i - box.size() + 1;
      box.erase(make_pair(LCP[pos], pos));
      mxv[pos] = box.begin()->first;
      occ[belong[sa[pos]]]--;
      distinct_cnt -= !occ[belong[sa[pos]]];
    }
  }
  box.clear();
  memset(occ, 0, sizeof(occ));
  distinct_cnt = 0;
  int lastout = -1;
  for (int i = 0; i < len; i++) {
    distinct_cnt += !occ[belong[sa[i]]];
    occ[belong[sa[i]]]++;
    box.insert(make_pair(LCP[i], i));
    box2.insert(make_pair(-mxv[i], i));
    ans[i] = -box2.begin()->first;
    while (distinct_cnt == k) {
      int pos = i - box.size() + 1;
      occ[belong[sa[pos]]]--;
      distinct_cnt -= !occ[belong[sa[pos]]];
      box.erase(make_pair(LCP[pos], pos));
      box2.erase(make_pair(-mxv[pos], pos));
      lastout = pos;
    }
    if (lastout != -1) ans[i] = max(ans[i], box.begin()->first);
  }
  for (int i = 0; i < len; i++) fans[belong[sa[i]]] += ans[i];
}
char second[maxN];
int main() {
  int n, k;
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%s", second);
    slen[i] = strlen(second);
    start[i] = len;
    s += second;
    s += "&";
    len += slen[i] + 1;
    if (k == 1) cout << (slen[i] * ((long long)(slen[i] + 1)) / 2) << " ";
  }
  if (k == 1) {
    cout << endl;
    exit(0);
  }
  mapstring(n, '&');
  buildsa();
  computeLCP();
  qry(k);
  for (int i = 1; i <= n; i++) printf("%I64d ", fans[i]);
  cout << endl;
}
