#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, r_max = 0, r_min = 105, u_min = 105, u_max = 0, flag = 0, cnt = 0,
            up, right;
  string s[105];
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> s[i];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i][j] == 'B') {
        flag = 1;
        r_min = min(j + 1, r_min);
        r_max = max(j + 1, r_max);
        u_min = min(i + 1, u_min);
        u_max = max(i + 1, u_max);
      }
    }
  }
  if (flag == 0) {
    cout << 1 << endl;
    return 0;
  }
  if ((r_max - r_min + 1) > n || (u_max - u_min + 1) > m) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if ((j + 1 >= r_min && j + 1 <= r_max)) {
        if ((i + 1 >= u_min && i + 1 <= u_max)) {
          if (s[i][j] == 'W') cnt++;
        }
      }
    }
  }
  up = (u_max - u_min + 1);
  right = (r_max - r_min + 1);
  if (up > right) {
    cnt += (up - right) * up;
  } else if (up < right) {
    cnt += (right - up) * right;
  }
  cout << cnt << endl;
  return 0;
}
