#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  char s1[n][m];
  int i, flag = 0, countt = 0, temp1 = 1000001, temp2 = 0, first1, last1,
         first2, last2, j, height, width, ans, total, flag2 = 0;
  for (i = 0; i < n; i++) scanf("%s", s1[i]);
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (flag == 0 && s1[i][j] == 'B') {
        first1 = i;
        last1 = i;
        flag = 1;
        flag2 = 1;
        break;
      } else if (flag != 0 && s1[i][j] == 'B')
        last1 = i;
    }
  }
  flag = 0;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      if (flag == 0 && s1[i][j] == 'B') {
        first2 = j;
        last2 = j;
        flag = 1;
        flag2 = 1;
        countt++;
        temp1 = min(first2, temp1);
        temp2 = max(last2, temp2);
        flag2 = 1;
      } else if (flag != 0 && s1[i][j] == 'B') {
        last2 = j;
        countt++;
        temp2 = max(last2, temp2);
        flag2 = 1;
      }
    }
    flag = 0;
  }
  if (flag2 == 0) {
    cout << "1" << endl;
  } else {
    height = last1 - first1 + 1;
    width = temp2 - temp1 + 1;
    if (height < width) {
      total = n - height;
      if (height + total >= width) {
        ans = width * width - countt;
        cout << ans;
      } else
        cout << "-1" << endl;
    } else if (height > width) {
      total = m - width;
      if (width + total >= height) {
        ans = height * height - countt;
        cout << ans;
      } else
        cout << "-1" << endl;
    } else {
      ans = width * width - countt;
      cout << ans;
    }
  }
}
