#include <bits/stdc++.h>
int main() {
  char in[102][102];
  int index[4], a, b, c, d, i, j, k, o, n, m, t = 0;
  index[0] = 0;
  index[1] = 0;
  index[2] = 0;
  index[3] = 0;
  scanf("%d%d", &n, &m);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      scanf(" %c", &in[i][j]);
      if (in[i][j] == 'B') {
        t++;
        if (j < index[0] || index[0] == 0) index[0] = j;
        if (j > index[1] || index[1] == 0) index[1] = j;
        if (i < index[2] || index[2] == 0) index[2] = i;
        if (i > index[3] || index[3] == 0) index[3] = i;
      }
    }
  }
  if (index[0] == 0 && index[1] == 0 && index[2] == 0 && index[3] == 0) {
    printf("1\n");
  } else {
    a = index[1] - index[0] + 1;
    b = index[3] - index[2] + 1;
    if (a >= b)
      c = a;
    else
      c = b;
    if (c > n || c > m)
      printf("-1\n");
    else {
      o = (c * c) - t;
      printf("%d\n", o);
    }
  }
}
