#include <bits/stdc++.h>
using namespace std;
char s[110][110];
int num[110][110];
int main() {
  int n, m;
  memset(num, 0, sizeof num);
  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')
        num[i][j] = num[i - 1][j] + num[i][j - 1] - num[i - 1][j - 1] + 1;
      else
        num[i][j] = num[i - 1][j] + num[i][j - 1] - num[i - 1][j - 1];
    }
  }
  int ans = -1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      for (int l = 0; i + l <= n && j + l <= m; l++) {
        int p = num[i + l][j + l] - num[i + l][j - 1] - num[i - 1][j + l] +
                num[i - 1][j - 1];
        if (p == num[n][m]) {
          if (ans == -1)
            ans = (l + 1) * (l + 1) - p;
          else
            ans = min(ans, (l + 1) * (l + 1) - p);
        }
      }
    }
  }
  printf("%d\n", ans);
  return 0;
}
