#include <bits/stdc++.h>
#pragma warning(disable : 4996 4018)
using namespace std;
int n, m;
int u = 111, d, l = 111, r, s;
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    for (int e = 0; e < m; e++) {
      char c;
      cin >> c;
      if (c == 'B') {
        u = min(u, i);
        d = max(d, i);
        l = min(l, e);
        r = max(r, e);
        s++;
      }
    }
  if (!s)
    cout << "1\n";
  else {
    int w = r - l + 1, h = d - u + 1;
    w = max(w, h);
    if (w > n || w > m)
      cout << "-1\n";
    else
      cout << w * w - s << '\n';
  }
  return 0;
}
