#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, i, j, hmin = INT_MAX, hmax = 0, bmin = INT_MAX, bmax = 0, c = 0;
  char arr[100][100];
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) cin >> arr[i][j];
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (arr[i][j] == 'B') {
        c++;
        if (i < hmin) {
          hmin = i;
        }
        if (i > hmax) {
          hmax = i;
        }
        if (j < bmin) {
          bmin = j;
        }
        if (j > bmax) {
          bmax = j;
        }
      }
    }
  }
  int max, min;
  if (hmax - hmin > bmax - bmin)
    max = hmax - hmin + 1;
  else
    max = bmax - bmin + 1;
  if (n > m)
    min = m;
  else
    min = n;
  if (c == 0)
    cout << "1" << endl;
  else {
    if (min < max)
      cout << "-1" << endl;
    else
      cout << max * max - c << endl;
  }
  return 0;
}
