#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void checkMin(T& a, T b) {
  if (a > b) a = b;
}
template <typename T>
inline void checkMax(T& a, T b) {
  if (a < b) a = b;
}
const int MAXN = 100100 * 2;
int N, K, id[MAXN], a[MAXN], n;
long long ans[MAXN / 2];
int SA[MAXN], RK[MAXN], HT[MAXN];
void suffix_array(int s[], int n) {
  static int buf[4][MAXN], w[MAXN];
  int *a = buf[0], *b = buf[1], *x = buf[2], *y = buf[3];
  static pair<int, int> pa[MAXN];
  for (int i = 0; i < n; ++i) {
    pa[i] = make_pair(s[i], i);
  }
  sort(pa, pa + n);
  for (int i = 0; i < n; ++i) {
    a[i] = pa[i].second;
  }
  x[a[0]] = 0, x[n] = y[n] = -1;
  for (int i = 1; i < n; ++i) {
    x[a[i]] = x[a[i - 1]] + (s[a[i - 1]] < s[a[i]]);
  }
  for (int k = 1; k < n && x[a[n - 1]] < n - 1; k *= 2) {
    for (int i = 0; i < n; ++i) {
      w[x[a[i]]] = i;
    }
    for (int i = n - 1; i >= 0; --i) {
      if (a[i] >= k) {
        b[w[x[a[i] - k]]--] = a[i] - k;
      }
    }
    for (int i = n - k; i < n; ++i) {
      b[w[x[i]]--] = i;
    }
    y[b[0]] = 0;
    for (int i = 1; i < n; ++i) {
      int p = b[i - 1], c = b[i];
      y[c] = y[p] + (x[p] < x[c] || x[p + k] < x[c + k]);
    }
    swap(a, b), swap(x, y);
  }
  copy(a, a + n, SA);
  copy(x, x + n, RK);
  HT[0] = 0;
  for (int i = 0, k = 0; i < n; ++i) {
    if (RK[i] != 0) {
      int j = SA[RK[i] - 1];
      while (i + k < n && j + k < n && s[i + k] == s[j + k]) {
        ++k;
      }
      HT[RK[i]] = k;
      k -= k > 0;
    }
  }
}
int main() {
  scanf("%d%d", &N, &K);
  static char s[MAXN / 2];
  for (int i = 0; i < int(N); ++i) {
    scanf("%s", s);
    if (K == 1) {
      long long l = strlen(s);
      ans[i] = (l * (l + 1)) >> 1;
    } else if (N < K) {
      ans[i] = 0;
    } else {
      for (int j = 0; s[j] != '\0'; ++j) {
        id[n] = i;
        a[n++] = s[j];
      }
      id[n] = i;
      a[n++] = -i;
    }
  }
  if (K != 1 && N >= K) {
    suffix_array(a, n);
    static int pre[MAXN];
    static int cnt[MAXN / 2];
    for (int l = 0, r = 0, k = 0; r < n; ++r) {
      if (cnt[id[SA[r]]]++ == 0) {
        ++k;
      }
      while (k > K) {
        if (--cnt[id[SA[l++]]] == 0) {
          --k;
        }
      }
      while (k == K && cnt[id[SA[l]]] > 1) {
        --cnt[id[SA[l++]]];
      }
      pre[r] = k == K ? l : -1;
    }
    static int lt[MAXN], rt[MAXN];
    for (int i = 0; i < n; ++i) {
      int j = i - 1;
      while (j >= 0 && HT[j] >= HT[i]) {
        j = lt[j];
      }
      lt[i] = j;
    }
    for (int i = n - 1; i >= 0; --i) {
      int j = i + 1;
      while (j < n && HT[j] >= HT[i]) {
        j = rt[j];
      }
      rt[i] = j;
    }
    for (int i = 0; i < n; ++i) {
      int l = i, r = i, temp = 0;
      while (pre[r] < l) {
        if (0 <= l && (r + 1 == n || HT[l] > HT[r + 1])) {
          temp = HT[l];
          l = lt[l];
        } else if (r + 1 < n) {
          temp = HT[r + 1];
          r = rt[r + 1] - 1;
        } else {
          temp = 0;
          break;
        }
      }
      ans[id[SA[i]]] += temp;
    }
  }
  for (int i = 0; i < int(N); ++i) {
    printf("%I64d%s", ans[i], i + 1 == N ? "\n" : " ");
  }
  return 0;
}
