#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const double eps = 1e-9;
char mapp[110][110];
int main() {
  cin.sync_with_stdio(false);
  int n, m;
  int ans, cnt;
  int x1, x2, y1, y2;
  while (cin >> n >> m) {
    ans = 0;
    x1 = 0x3f3f3f3f, x2 = -1, y1 = 0x3f3f3f3f, y2 = -1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> mapp[i][j];
        if (mapp[i][j] == 'B') {
          x1 = min(x1, i);
          y1 = min(y1, j);
          x2 = max(x2, i);
          y2 = max(y2, j);
          ans++;
        }
      }
    }
    if (x1 == 0x3f3f3f3f) {
      cout << 1 << endl;
    } else {
      cnt = max(x2 - x1, y2 - y1) + 1;
      if (cnt > n || cnt > m) {
        cout << -1 << endl;
        continue;
      }
      cout << cnt * cnt - ans << endl;
    }
  }
  return 0;
}
