#include <bits/stdc++.h>
using namespace std;
const int MX = 2e5 + 5;
const int mod = 1e9 + 7;
const long long int inf = 1LL << 62;
int dx4[] = {0, 0, -1, 1};
int dy4[] = {1, -1, 0, 0};
int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
int dy[] = {1, 0, -1, 1, -1, 1, 0, -1};
char ch[105][105];
int main() {
  long long int n, m, mn = inf, b = 0;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> ch[i][j];
      if (ch[i][j] == 'B') b++;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      long long int o = 0;
      for (int cnt = 0; cnt <= n; cnt++) {
        if (i + cnt > n || j + cnt > m) {
          break;
        }
        for (int ii = i; ii <= i + cnt; ii++) {
          if (ch[ii][j + cnt] == 'B') o++;
        }
        for (int jj = j; jj <= j + cnt - 1; jj++) {
          if (ch[i + cnt][jj] == 'B') o++;
        }
        if (o == b) mn = min(mn, (cnt + 1) * (cnt + 1) - b);
      }
    }
  }
  cout << (mn == inf ? -1 : mn) << endl;
  return 0;
}
