#include <bits/stdc++.h>
using namespace std;
class code {
 public:
  long long a, b;
};
code arr[105 * 105], brr[105 * 105];
bool cmp1(code c1, code c2) {
  if (c1.a == c2.a)
    return c1.b < c2.b;
  else
    return c1.a < c2.a;
}
bool cmp2(code c1, code c2) {
  if (c1.b == c2.b)
    return c1.a < c2.a;
  else
    return c1.b < c2.b;
}
int main() {
  long long n, m;
  char str[105][105];
  scanf("%lld %lld", &n, &m);
  for (long long i = 0; i < n; i++) {
    scanf("%s", str[i]);
  }
  long long k = 0;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      if (str[i][j] == 'B') {
        arr[k].a = i;
        arr[k].b = j;
        brr[k].a = i;
        brr[k].b = j;
        k++;
      }
    }
  }
  if (k == 1)
    printf("0\n");
  else if (k == 0)
    printf("1\n");
  else {
    sort(arr, arr + k, cmp1);
    sort(brr, brr + k, cmp2);
    long long c1 = arr[0].a;
    long long c2 = arr[k - 1].a;
    long long d1 = min(brr[0].b, min(arr[0].b, arr[k - 1].b));
    long long d2 = max(brr[k - 1].b, max(arr[0].b, arr[k - 1].b));
    long long cnt = 0;
    for (long long i = c1; i <= c2; i++) {
      for (long long j = d1; j <= d2; j++) {
        if (str[i][j] == 'W') cnt++;
      }
    }
    long long dif1 = c2 - c1 + 1;
    long long dif2 = d2 - d1 + 1;
    long long mx = fabs(dif1 - dif2);
    long long kk = max(dif1, dif2);
    cnt = cnt + (mx * kk);
    if (n < kk || m < kk)
      printf("-1\n");
    else
      printf("%lld\n", cnt);
  }
  return (0);
}
