#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
char s[1000][1000];
int main() {
  if (fopen("/home/lianyi/123.txt", "r") != NULL) {
    freopen("/home/lianyi/123.txt", "r", stdin);
  }
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  while (cin >> n >> m) {
    for (int i = 1; i <= n; i++) {
      cin >> (s[i] + 1);
    }
    int up = inf, down = -inf, l = inf, r = -inf;
    int flag = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (s[i][j] == 'B') {
          up = min(up, i);
          down = max(down, i);
          l = min(l, j);
          r = max(r, j);
          flag = 1;
        }
      }
    }
    if (flag == 0) {
      cout << 1 << endl;
      continue;
    }
    int len1 = (down - up + 1), len2 = (r - l + 1);
    int cnt = 0;
    for (int i = up; i <= down; i++) {
      for (int j = l; j <= r; j++) {
        if (s[i][j] == 'W') {
          cnt++;
        }
      }
    }
    if (len1 > len2) {
      if (m >= len1) {
        cnt += (len1 - len2) * len1;
        cout << cnt << '\n';
      } else {
        cout << -1 << '\n';
      }
    } else {
      if (n >= len2) {
        cnt += (len2 - len1) * len2;
        cout << cnt << '\n';
      } else {
        cout << -1 << '\n';
      }
    }
  }
  return 0;
}
