#include <bits/stdc++.h>
using namespace std;
int n, m, x = 1e9, y = 1e9, xx = 0, yy = 0;
const int N = 105;
char g[N][N];
int check(int x, int y, int k) {
  int res = 0;
  for (int i = x; i <= x + k; i++)
    for (int j = y; j <= y + k; j++)
      if (g[i][j] == 'W') res++;
  return res;
}
int main() {
  scanf("%d %d", &n, &m);
  bool fd = false;
  for (int i = 1; i <= n; i++) {
    scanf(" %s", g[i] + 1);
    for (int j = 1; j <= m; j++)
      if (g[i][j] == 'B') {
        fd = true;
        x = min(x, i);
        y = min(y, j);
        xx = max(xx, i);
        yy = max(yy, j);
      }
  }
  if (!fd) {
    puts("1");
    return 0;
  }
  int ans = 1e9;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (i <= x && j <= y)
        for (int k = xx - i; i + k <= n && j + k <= m; k++)
          if (i + k >= xx && j + k >= yy) ans = min(ans, check(i, j, k));
  printf("%d\n", (ans == 1e9 ? -1 : ans));
  return 0;
}
