#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  vector<string> b(n);
  for (auto& r : b) cin >> r;
  int black = 0, DP[101][101] = {};
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      if (b[i][j] == 'B') black++, DP[i][j]++;
  for (int j = 1; j < m; j++) DP[0][j] += DP[0][j - 1];
  for (int i = 1; i < n; i++) DP[i][0] += DP[i - 1][0];
  for (int i = 1; i < n; i++)
    for (int j = 1; j < m; j++)
      DP[i][j] = DP[i][j] + DP[i - 1][j] + DP[i][j - 1] - DP[i - 1][j - 1];
  if (black == 0) return cout << 1, 0;
  int ats = INT_MAX;
  for (int r1 = 0; r1 < n; r1++)
    for (int r2 = r1; r2 < n; r2++)
      for (int c1 = 0; c1 < m; c1++)
        for (int c2 = c1; c2 < m; c2++) {
          int bl = DP[r2][c2];
          if (r1 != 0) bl -= DP[r1 - 1][c2];
          if (c1 != 0) bl -= DP[r2][c1 - 1];
          if (r1 != 0 && c1 != 0) bl += DP[r1 - 1][c1 - 1];
          if (c2 - c1 == r2 - r1 && bl == black)
            ats = min(ats, (r2 - r1 + 1) * (c2 - c1 + 1) - bl);
        }
  cout << (ats == INT_MAX ? -1 : ats) << "\n";
}
