#include <bits/stdc++.h>
int main() {
  char s[100][100];
  int i, j, n, m, flag = 0, max;
  scanf("%d %d", &n, &m);
  getchar();
  int a = 0, b = 0, c = 2100, d = 0;
  for (i = 0; i < n; i++) {
    scanf("%s", s[i]);
    for (j = 0; j < m; j++) {
      if (s[i][j] == 'B') {
        flag++;
        b = i;
        if (j < c) c = j;
        if (j > d) d = j;
        if (flag == 1) a = i;
      }
    }
  }
  int x;
  x = b - a + 1;
  int y;
  y = d - c + 1;
  if (x > y)
    max = x;
  else
    max = y;
  if (x > m || y > n)
    printf("-1\n");
  else if (flag == 0)
    printf("1\n");
  else
    printf("%d", max * max - flag);
  return 0;
}
