#include <bits/stdc++.h>
using namespace std;
int a, b, n, c, d, t, ans, m;
char s[220];
int main() {
  scanf("%d%d", &n, &m);
  t = 0;
  a = n;
  b = -1;
  c = m;
  d = -1;
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    for (int j = 0; j < m; j++)
      if (s[j] == 'B') {
        a = min(a, i);
        b = max(b, i);
        c = min(c, j);
        d = max(d, j);
        t++;
      }
  }
  if (t == 0)
    printf("1\n");
  else if (d - c + 1 > n || b - a + 1 > m)
    printf("-1\n");
  else {
    ans = max(d - c + 1, b - a + 1);
    printf("%d\n", ans * ans - t);
  }
  return 0;
}
