#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<int> a[111];
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    a[i].resize(m);
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      if (c == 'B')
        a[i][j] = 1;
      else
        a[i][j] = 0;
    }
  }
  int xmin = 200, xmax = -200, ymin = 200, ymax = -200, k = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == 1) {
        xmin = min(xmin, i);
        xmax = max(xmax, i);
        ymin = min(ymin, j);
        ymax = max(ymax, j);
        k++;
      }
    }
  }
  if (xmin == 200) {
    cout << 1;
    return 0;
  }
  int width = xmax - xmin + 1, heigth = ymax - ymin + 1;
  if (max(width, heigth) > n || max(width, heigth) > m) {
    cout << -1;
    return 0;
  }
  cout << max(width, heigth) * max(width, heigth) - k;
  return 0;
}
