#include <bits/stdc++.h>
using namespace std;
int n, m, minx = 1 << 20, miny = 1 << 20, maxx = -1, maxy = -1;
char s[200][200];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s[i] + 1);
    for (int j = 1; j <= m; j++)
      if (s[i][j] == 'B') {
        minx = min(minx, i);
        miny = min(miny, j);
        maxx = max(maxx, i);
        maxy = max(maxy, j);
      }
  }
  if (maxx < 0) {
    puts("1");
    return 0;
  }
  int ans = 1 << 20, len = max(maxx - minx + 1, maxy - miny + 1);
  if (len > n || len > m) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i <= minx && i + len <= n + 1; i++)
    for (int j = 1; j <= miny && j + len <= m + 1; j++) {
      if (i + len <= maxx || j + len <= maxy) continue;
      int cnt = 0;
      for (int k = i; k < i + len; k++)
        for (int l = j; l < j + len; l++)
          if (s[k][l] == 'W') cnt++;
      ans = min(ans, cnt);
    }
  printf("%d\n", ans > 10000 ? -1 : ans);
  return 0;
}
