#include <bits/stdc++.h>
using namespace std;
int n, m, x, y, z, ans;
vector<int> rows[200], cols[200];
string str[300];
int uporerrow = 10000, nicherrow, dancol, bamcol = 10000;
int cnt = 0;
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> str[i];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (str[i][j] == 'B') {
        uporerrow = min(uporerrow, i);
        nicherrow = max(nicherrow, i);
        dancol = max(dancol, j);
        bamcol = min(bamcol, j);
        cnt++;
      }
    }
  }
  if (cnt == 0) {
    cout << 1 << endl;
    return 0;
  }
  int tmp1 = nicherrow - uporerrow + 1;
  int tmp2 = dancol - bamcol + 1;
  if (tmp1 > tmp2) {
    int xx1 = bamcol + (m - dancol - 1);
    if (xx1 < (tmp1 - tmp2)) {
      cout << -1 << endl;
      return 0;
    }
    int xx = tmp1 - tmp2;
    if (bamcol >= xx) {
      bamcol = bamcol - xx;
    } else {
      xx -= bamcol;
      bamcol = 0;
      dancol = dancol + xx;
    }
  } else if (tmp1 < tmp2) {
    int xx1 = uporerrow + (n - nicherrow - 1);
    if (xx1 < (tmp2 - tmp1)) {
      cout << -1 << endl;
      return 0;
    }
    int xx = tmp2 - tmp1;
    if (uporerrow >= xx) {
      uporerrow = uporerrow - xx;
    } else {
      xx -= uporerrow;
      uporerrow = 0;
      nicherrow = nicherrow + xx;
    }
  }
  for (int i = uporerrow; i <= nicherrow; i++) {
    for (int j = bamcol; j <= dancol; j++) {
      if (str[i][j] == 'W') ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
