#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  std::cin >> n >> m;
  std::vector<string> v(n);
  for (long long i = 0; i < n; i++) {
    std::cin >> v[i];
  }
  long long cb = 0;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < m; j++) {
      if (v[i][j] == 'B') cb++;
    }
  }
  bool f = true;
  long long ans = 0;
  long long tcb = 0;
  for (long long s = 1; f && s <= min(n, m); s++) {
    for (long long i = 0; f && i <= n - s; i++) {
      for (long long j = 0; f && j <= m - s; j++) {
        ans = 0;
        tcb = 0;
        for (long long k = i; f && k < i + s; k++) {
          for (long long l = j; f && l < j + s; l++) {
            if (v[k][l] == 'W')
              ans++;
            else
              tcb++;
          }
        }
        if (tcb >= cb) f = false;
      }
    }
  }
  if (f)
    std::cout << "-1" << '\n';
  else
    std::cout << ans << '\n';
  return 0;
}
