#include <bits/stdc++.h>
using namespace std;
char mp[202][202];
int main() {
  int n, m;
  while (~scanf("%d%d", &n, &m)) {
    for (int i = 1; i <= n; i++) scanf("%s", mp[i] + 1);
    int maxx = 0, maxy = 0, minx = 200, miny = 200;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        if (mp[i][j] == 'B')
          maxx = max(maxx, i), maxy = max(maxy, j), minx = min(minx, i),
          miny = min(miny, j);
    if (maxx == 0) {
      printf("1\n");
      continue;
    }
    int len = max(maxx - minx + 1, maxy - miny + 1);
    int maxlen = min(n, m);
    if (len > maxlen)
      printf("-1\n");
    else {
      int ans = 0;
      for (int i = minx; i <= maxx; i++)
        for (int j = miny; j <= maxy; j++)
          if (mp[i][j] == 'W') ans++;
      ans += (len - (maxx - minx + 1)) * len;
      ans += (len - (maxy - miny + 1)) * len;
      printf("%d\n", ans);
    }
  }
}
