#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i, j, sx, ex, sy, ey, check = 0, count = 0, c;
  char s;
  cin >> n >> m;
  ex = 1;
  sx = n;
  ey = 1;
  sy = m;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      cin >> s;
      if (s == 'B') {
        count++;
        check = 1;
        if (i < sx) sx = i;
        if (i > ex) ex = i;
        if (j < sy) sy = j;
        if (j > ey) ey = j;
      }
    }
  }
  if (!check) {
    cout << 1;
    return 0;
  }
  if (ex - sx > ey - sy)
    c = ex - sx + 1;
  else
    c = ey - sy + 1;
  if (c > m || c > n)
    cout << -1;
  else
    cout << c * c - count;
  return 0;
}
