#include <bits/stdc++.h>
using namespace std;
const double _pi = acos(-1.0);
int ar[110][110];
int main() {
  int n, m, c = 0, res = -1;
  scanf("%d %d\n", &n, &m);
  vector<string> v(n);
  for (int i = 0; i < n; i++) {
    getline(cin, v[i]);
    for (int j = 0; j < m; j++) {
      ar[i + 1][j + 1] = ar[i][j + 1] + ar[i + 1][j] - ar[i][j];
      if (v[i][j] == 'B') {
        c++;
        ar[i + 1][j + 1]++;
      }
    }
  }
  for (int i = 1; i <= min(n, m); i++) {
    for (int j = 0; j <= n - i; j++) {
      for (int k = 0; k <= m - i; k++) {
        int mc = ar[j + i][k + i] + ar[j][k] - ar[j][k + i] - ar[j + i][k];
        if (mc == c) {
          if (res == -1)
            res = i * i - mc;
          else
            res = min(res, i * i - mc);
        }
      }
    }
  }
  printf("%d\n", res);
  return 0;
}
