#include <bits/stdc++.h>
using namespace std;
vector<int> x, y;
char s[105][105];
int main() {
  int n, m;
  cin >> n >> m;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    scanf(" %s", s[i]);
    for (int j = 0; j < m; j++) {
      if (s[i][j] == 'B') {
        cnt++;
        x.push_back(i);
        y.push_back(j);
      }
    }
  }
  if (cnt == 0) return 0 * printf("1\n");
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  int l = max(x.back() - x[0], y.back() - y[0]) + 1;
  if (l > n || l > m)
    cout << -1 << endl;
  else
    cout << l * l - cnt << endl;
  return 0;
}
