#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int min_i = n - 1, max_i = 0;
  int max_j = 0, min_j = m - 1;
  int b = 0;
  bool flag = true;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      char c;
      cin >> c;
      if (c == 'B') {
        min_i = min(min_i, i);
        max_i = max(max_i, i);
        min_j = min(min_j, j);
        max_j = max(max_j, j);
        flag = false;
        b++;
      }
    }
  }
  if (flag) {
    cout << 1;
    return 0;
  }
  int width = max_j - min_j + 1, height = max_i - min_i + 1;
  if (width > n || height > m) {
    cout << -1;
    return 0;
  }
  int ans = max(width, height);
  cout << ans * ans - b;
  return 0;
}
