#include <bits/stdc++.h>
using namespace std;
char celule[102][102];
int n, m;
int minx, miny, maxx, maxy, cnt, nrcel, len;
int main() {
  scanf("%d%d", &n, &m);
  minx = miny = 1e9;
  maxx = maxy = 0;
  cnt = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("\n%c", &celule[i][j]);
      if (celule[i][j] == 'B') {
        cnt++;
        if (i < minx) minx = i;
        if (j < miny) miny = j;
        if (i > maxx) maxx = i;
        if (j > maxy) maxy = j;
      }
    }
  }
  len = max(maxx - minx, maxy - miny);
  nrcel = 1e9;
  bool ok = 1;
  if (cnt == n * m && (n * m != m * m && n * m != n * n)) ok = 0;
  if (len + 1 > m || len + 1 > n) ok = 0;
  if (ok && !(maxx == maxy && maxy == 0)) {
    cnt = 0;
    for (int addlin = 0; addlin <= n - len - 1; addlin++) {
      for (int addcol = 0; addcol <= m - len - 1; addcol++) {
        for (int i = 1 + addlin; i <= len + 1 + addlin; i++) {
          for (int j = 1 + addcol; j <= len + 1 + addcol; j++) {
            if (celule[i][j] == 'W') cnt++;
          }
        }
        if (cnt < nrcel) nrcel = cnt;
        cnt = 0;
      }
    }
    if (maxx == maxy && maxy == 0) nrcel = 1;
  } else {
    nrcel = -1;
    if (maxx == maxy && maxy == 0) nrcel = 1;
  }
  printf("%d", nrcel);
  return 0;
}
