#include <bits/stdc++.h>
using namespace std;
char s[101][103];
int prefix[101][101];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int minx = 1000000000, maxx = 0, miny = 1000000000, maxy = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s[i] + 1);
    for (int j = 1; j <= m; ++j) {
      prefix[i][j] = prefix[i][j - 1] + (s[i][j] == 'B' ? 1 : 0);
      if (s[i][j] == 'B') {
        if (i < minx) minx = i;
        if (i > maxx) maxx = i;
        if (j < miny) miny = j;
        if (j > maxy) maxy = j;
      }
    }
  }
  int ans = 1000000000;
  for (int sx = 1; sx <= n; ++sx)
    for (int sy = 1; sy <= m; ++sy)
      for (int len = 1; len <= n && len <= m; ++len)
        if (sx + len - 1 <= n && sy + len - 1 <= m && sx <= minx &&
            sx + len - 1 >= maxx && sy <= miny && sy + len - 1 >= maxy) {
          int tmp = 0;
          for (int i = sx; i < sx + len; ++i)
            tmp += prefix[i][sy + len - 1] - prefix[i][sy - 1];
          if (len * len - tmp < ans) ans = len * len - tmp;
        }
  printf("%d", ans == 1000000000 ? -1 : ans);
  return 0;
}
