#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int m1, m2, k = 0;
  vector<int> v1, v2;
  char a[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'B') {
        k++;
        v1.push_back(i);
        v2.push_back(j);
      }
    }
  }
  sort((v1).begin(), (v1).end());
  sort((v2).begin(), (v2).end());
  if (k == 0) return cout << "1", 0;
  m1 = v1[v1.size() - 1] - v1[0] + 1;
  m2 = v2[v2.size() - 1] - v2[0] + 1;
  if (m1 > m) return cout << "-1", 0;
  if (m2 > n) return cout << "-1", 0;
  cout << max(m1, m2) * max(m1, m2) - k;
}
