#include <bits/stdc++.h>
using namespace std;
int a[101][101];
int main() {
  int w, h;
  cin >> h >> w;
  char c;
  int s = 1000000, n = -1, wx = -1, e = 100000;
  int check = 1;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      cin >> c;
      if (c == 'B') {
        check = 0;
        a[i][j] = 1;
        if (j > n) {
          n = j;
        }
        if (j < s) {
          s = j;
        }
        if (i < e) {
          e = i;
        }
        if (i > wx) {
          wx = i;
        }
      }
    }
  if (check) {
    cout << "1";
    return 0;
  }
  if (n - s > wx - e) {
    int add = n - s - wx + e;
    int res = 10000000;
    for (int start = e - add; start <= e; start++) {
      if (start >= 0 && start + add + wx - e < h) {
        int kek = 0;
        for (int i = start; i <= start + add + wx - e; i++)
          for (int j = s; j <= n; j++)
            if (!a[i][j]) {
              kek++;
            }
        res = min(res, kek);
      }
    }
    if (res == 10000000) {
      cout << "-1";
      return 0;
    }
    cout << res;
  } else {
    int add = -(n - s - wx + e);
    int res = 10000000;
    for (int start = s - add; start <= s; start++) {
      if (start >= 0 && start + add + n - s < w) {
        int kek = 0;
        for (int i = e; i <= wx; i++)
          for (int j = start; j <= start + add + n - s; j++)
            if (!a[i][j]) {
              kek++;
            }
        res = min(res, kek);
      }
    }
    if (res == 10000000) {
      cout << "-1";
      return 0;
    }
    cout << res;
  }
  return 0;
}
