#include <bits/stdc++.h>
using namespace std;
int n, m;
string grid[101];
int nb, l = 200, r = -1, u = 200, d = -1;
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) cin >> grid[i];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++)
      if (grid[i][j] == 'B') {
        nb++;
        l = min(l, j);
        r = max(r, j);
        u = min(u, i);
        d = max(d, i);
      }
  }
  if (nb == 0) {
    cout << 1 << endl;
    return 0;
  }
  int h = d - u + 1, w = r - l + 1;
  if (w > h) {
    if (w > n) {
      cout << -1 << endl;
      return 0;
    }
    cout << w * w - nb << endl;
  } else {
    if (h > m) {
      cout << -1 << endl;
      return 0;
    }
    cout << h * h - nb << endl;
  }
}
