#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m) {
    char M[n + 10][m + 10];
    int cnt = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> M[i][j];
        if (M[i][j] == 'B') cnt++;
      }
    }
    if (!cnt) {
      cout << "1\n";
    } else {
      int minX, maxX, maxY, minY;
      bool sw = true;
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if (sw and M[i][j] == 'B') {
            minX = i;
            sw = false;
          }
          if (M[i][j] == 'B') {
            maxX = i;
          }
        }
      }
      sw = true;
      for (int j = 0; j < m; j++) {
        for (int i = 0; i < n; i++) {
          if (sw and M[i][j] == 'B') {
            minY = j;
            sw = false;
          }
          if (M[i][j] == 'B') {
            maxY = j;
          }
        }
      }
      int d = max(maxX - minX + 1, maxY - minY + 1);
      if (d > n or d > m) {
        cout << "-1\n";
      } else {
        cout << (d * d) - cnt << "\n";
      }
    }
  }
  return 0;
}
