#include <bits/stdc++.h>
char a[2000][2000];
int main() {
  int m, n, m1, m2, n1, n2;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) {
    scanf("%s", a[i]);
  }
  int maxh = 0, maxs = 0, minh = 200, mins = 200;
  int sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 'B') {
        sum++;
      }
      if (a[i][j] == 'B' && i < mins) {
        mins = i;
      }
      if (a[i][j] == 'B' && i > maxs) {
        maxs = i;
      }
      if (a[i][j] == 'B' && j < minh) {
        minh = j;
      }
      if (a[i][j] == 'B' && j > maxh) {
        maxh = j;
      }
    }
  }
  int biaoji1, biaoji2;
  biaoji1 = maxs - mins + 1;
  biaoji2 = maxh - minh + 1;
  int s;
  if (biaoji1 >= biaoji2) {
    s = biaoji1;
  } else {
    s = biaoji2;
  }
  if (s > m || s > n) {
    printf("-1\n");
  } else {
    if (sum == 0) {
      printf("1\n");
    } else {
      printf("%d\n", s * s - sum);
    }
  }
  return 0;
}
