#include <cstdio>
#include <cassert>
#include <cstring>
#include <algorithm>

using namespace std;

#define MAXN 20
#define MAXK 20
#define MAXL 1000

int n, k;
char s[MAXL];
char a[MAXN][MAXL];
char x[MAXL];
int l[MAXN];

int best_res = -1;

char swap_color (int c) {
  return c == 'W' ? 'B' : 'W';
}

void f (int depth, int cur_res, int games) {
  if (cur_res + (k - games) <= best_res) {
    return;
  }
  if (games == k) {
    assert (cur_res > best_res);
    best_res = cur_res;
  }
  const int opponent = depth % n;
  int i = games - 1, j = l[opponent] - 1;
  char *c = &a[opponent][0];
  while (i >= 0 && j >= 0 && c[j] == x[i]) {
    i--;
    j--;
  }
  char start_color = 0;
  if (i >= 0 && j >= 0) {
    start_color = c[j];
  } else {
    start_color = 'W';
  }
  char opponent_color = swap_color (start_color);
  const int old_l = l[opponent];
  for (int o = 1; o <= 2 && games < k; o++) {
    c[l[opponent]++] = opponent_color;
    x[games++] = start_color;
    if (start_color == 'W') {
      cur_res++;
    }
    f (depth + 1, cur_res, games);
    swap (start_color, opponent_color);
  }
  l[opponent] = old_l;
}

int main (void) {
  int i;
  freopen ("checkers.in", "rt", stdin);
  freopen ("checkers.out", "wt", stdout);

  scanf ("%d%d", &n, &k);
  for (i = 0; i < n; i++) {
    scanf ("%s", s);
    l[i] = strlen (s);
    memcpy (&a[i][0], s, l[i]);
  }

  f (0, 0, 0);

  printf ("%d\n", best_res);
  return 0;
}
