#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long INFF = 0x3f3f3f3f3f3f3f3fll;
const int MAX = 1e5 + 5;
const long long MOD = 2012;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
template <typename T>
inline T abs(T a) {
  return a > 0 ? a : -a;
}
template <class T>
inline void read(T& num) {
  bool start = false, neg = false;
  char c;
  num = 0;
  while ((c = getchar()) != EOF) {
    if (c == '-')
      start = neg = true;
    else if (c >= '0' && c <= '9') {
      start = true;
      num = num * 10 + c - '0';
    } else if (start)
      break;
  }
  if (neg) num = -num;
}
inline long long powMM(long long a, long long b, long long M) {
  long long ret = 1;
  a %= M;
  while (b) {
    if (b & 1) ret = ret * a % M;
    b >>= 1;
    a = a * a % M;
  }
  return ret;
}
void open() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
int n, k;
const int MAXL = 1e6 + 5;
const int MAXCH = 26;
const char CH = 'a';
set<int> ok[MAXL];
char a[MAXL], b[MAXL];
int l[MAXL], r[MAXL], pre[MAXL], id[MAXL], cnt[MAXL];
struct SAM {
  int tot;
  int maxlen[MAXL], minlen[MAXL], trans[MAXL][MAXCH], slink[MAXL];
  int new_state(int _maxlen, int _minlen, int* _trans, int _slink) {
    maxlen[tot] = _maxlen;
    minlen[tot] = _minlen;
    slink[tot] = _slink;
    for (int i = 0; i < MAXCH; i++)
      trans[tot][i] = (_trans == NULL ? -1 : _trans[i]);
    return tot++;
  }
  int add_char(char ch, int u) {
    int c = ch - CH;
    if (trans[u][c] != -1 && maxlen[trans[u][c]] == maxlen[u] + 1)
      return trans[u][c];
    int z = new_state(maxlen[u] + 1, -1, NULL, -1);
    int v = u;
    while (v != -1 && trans[v][c] == -1) trans[v][c] = z, v = slink[v];
    if (v == -1) {
      slink[z] = 0;
      minlen[z] = 1;
      return z;
    }
    int x = trans[v][c];
    if (maxlen[v] + 1 == maxlen[x]) {
      slink[z] = x;
      minlen[z] = maxlen[x] + 1;
      return z;
    }
    int y = new_state(maxlen[v] + 1, maxlen[slink[x]] + 1, trans[x], slink[x]);
    slink[z] = slink[x] = y;
    minlen[z] = minlen[x] = maxlen[y] + 1;
    while (v != -1 && trans[v][c] == x) trans[v][c] = y, v = slink[v];
    return z;
  }
  int addchar(char ch, int u) { return trans[u][ch - 'a']; }
} sam;
int da;
long long an;
int main() {
  scanf("%d%d", &n, &k);
  r[0] = -1;
  int pres = 0;
  sam.tot = 0;
  pres = sam.new_state(0, 0, NULL, -1);
  for (int i = 1; i <= n; i++) {
    scanf("%s", b);
    pres = 0;
    int len = strlen(b);
    da = max(da, len);
    l[i] = r[i - 1] + 1;
    int st = l[i];
    for (int j = 0; j < len; j++) {
      a[st + j] = b[j];
      pres = sam.add_char(b[j], pres);
    }
    r[i] = l[i] + len - 1;
  }
  if (k > n) {
    for (int i = 1; i <= n; i++) printf("0 ");
    return 0 * printf("\n");
  }
  for (int i = 1; i <= n; i++) {
    pres = 0;
    for (int j = l[i]; j <= r[i]; j++) {
      pres = sam.addchar(a[j], pres);
      ok[pres].insert(i);
    }
  }
  for (int i = 1; i < sam.tot; i++) da = max(da, sam.maxlen[i]);
  for (int i = 1; i < sam.tot; i++) ++pre[sam.maxlen[i]];
  for (int i = 1; i < sam.tot; i++) pre[i] += pre[i - 1];
  for (int i = 1; i < sam.tot; i++) id[pre[sam.maxlen[i]]--] = i;
  for (int i = sam.tot - 1; i >= 1; i--) {
    int who = id[i];
    cnt[who] = ok[who].size();
    int fa = sam.slink[who];
    if (ok[fa].size() < ok[who].size()) swap(ok[fa], ok[who]);
    for (set<int>::iterator it = ok[who].begin(); it != ok[who].end(); it++)
      ok[fa].insert(*it);
  }
  for (int i = 1; i <= n; i++) {
    pres = 0;
    for (int j = l[i]; j <= r[i]; j++) {
      pres = sam.addchar(a[j], pres);
      int now = pres;
      while (now > 0) {
        if (cnt[now] >= k) {
          an += min(j - l[i] + 1, sam.maxlen[now]);
          break;
        }
        now = sam.slink[now];
      }
    }
    printf("%I64d \n", an);
    an = 0;
  }
}
