#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  int lx = n, rx = -1, ly = m, ry = -1;
  int b = 0;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < m; j++) {
      if (s[j] == 'B') {
        b++;
        lx = min(lx, i);
        rx = max(rx, i);
        ly = min(ly, j);
        ry = max(ry, j);
      }
    }
  }
  int len = max<int>({b == 0, rx - lx + 1, ry - ly + 1});
  cout << (len <= min(n, m) ? len * len - b : -1) << '\n';
  return 0;
}
