#include <bits/stdc++.h>
using namespace std;
int max(int a, int b) { return a > b ? a : b; }
int min(int a, int b) { return a < b ? a : b; }
struct Point {
  int y, x;
  Point(int y, int x) : y(y), x(x) {}
  Point() {}
};
void func() {
  int n, m;
  cin >> n >> m;
  int xMin = 101, xMax = 0, yMin = 101, yMax = 0;
  Point xMi, xMa, yMi, yMa;
  char c;
  bool flag = false;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> c;
      if (c == 'B') {
        cnt++;
        if (xMin > j) {
          xMin = j;
          xMi = Point{i, j};
        }
        if (xMax < j) {
          xMax = j;
          xMa = Point{i, j};
        }
        if (yMin > i) {
          yMin = i;
          yMi = Point{i, j};
        }
        if (yMax < i) {
          yMax = i;
          yMa = Point{i, j};
        }
        flag = true;
      }
    }
  }
  int len = 0;
  if (!flag) {
    cout << 1;
    return;
  }
  flag = false;
  if (xMax - xMin > yMax - yMin) {
    len = xMax - xMin;
    if (yMin + len < n || yMax - len >= 0)
      flag = true;
    else
      for (int i = 0; i < n - len; i++) {
        if ((yMa.y >= 0 && yMa.y < i + len) &&
            (yMi.y >= 0 && yMi.y < i + len)) {
          flag = true;
          break;
        }
      }
  } else {
    len = yMax - yMin;
    if (xMin + len < m || xMax - len >= 0)
      flag = true;
    else
      for (int i = 0; i < m - len; i++) {
        if ((xMa.x >= 0 && xMa.x < i + len) &&
            (xMi.x >= 0 && xMi.x < i + len)) {
          flag = true;
          break;
        }
      }
  }
  if (flag)
    cout << (len + 1) * (len + 1) - cnt;
  else
    cout << -1;
}
int main() {
  func();
  return 0;
}
