Problem p01139 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG_MODE
#define DBG(n) n;
#else
#define DBG(n) ;
#endif
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int WWW = 0; WWW < (n); WWW++)                                        \\
      cerr << v[WWW] << ' ';                                                   \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int aaa = 0; aaa < i; aaa++) {                                        \\
      for (int bbb = 0; bbb < j; bbb++)                                        \\
        cerr << v[aaa][bbb] << ' ';                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

typedef long long ll;
typedef pair<ll, ll> P;
char mp[55][55];
int n, m, bb, ww;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
void hoge(int h, int w) {
  queue<pair<int, int>> q;
  bool isb = false, isw = false;
  q.push(MP(h, w));
  int ret = 0;
  while (!q.empty()) {
    auto now = q.front();
    q.pop();
    if (mp[now.FI][now.SE] == '#')
      continue;
    mp[now.FI][now.SE] = '#';
    ret++;
    REP(i, 4) {
      int yy = now.FI + dy[i];
      int xx = now.SE + dx[i];
      if (xx < 0 || yy < 0 || xx >= m || yy >= n)
        continue;
      if (mp[yy][xx] == '.')
        q.push(MP(yy, xx));
      if (mp[yy][xx] == 'B')
        isb = true;
      if (mp[yy][xx] == 'W')
        isw = true;
    }
  }
  if ((isb & isw) || !(isb | isw))
    return;
  if (isb)
    bb += ret;
  else
    ww += ret;
}

int main() {
  while (cin >> m >> n, n | m) {
    bb = 0, ww = 0;
    REP(i, n) REP(j, m) cin >> mp[i][j];
    REP(i, n) REP(j, m) {
      if (mp[i][j] == '.')
        hoge(i, j);
    }
    cout << bb << ' ' << ww << endl;
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#ifdef DEBUG_MODE
#define DBG(n) n;
#else
#define DBG(n) ;
#endif
#define REP(i, n) for (ll(i) = (0); (i) < (n); ++i)
#define rep(i, s, g) for (ll(i) = (s); (i) < (g); ++i)
#define rrep(i, s, g) for (ll(i) = (s); i >= (g); --(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v, n)                                                           \\
  {                                                                            \\
    for (int WWW = 0; WWW < (n); WWW++)                                        \\
      cerr << v[WWW] << ' ';                                                   \\
    cerr << endl << endl;                                                      \\
  }
#define SHOW2d(v, i, j)                                                        \\
  {                                                                            \\
    for (int aaa = 0; aaa < i; aaa++) {                                        \\
      for (int bbb = 0; bbb < j; bbb++)                                        \\
        cerr << v[aaa][bbb] << ' ';                                            \\
      cerr << endl;                                                            \\
    }                                                                          \\
    cerr << endl;                                                              \\
  }
#define ALL(v) v.begin(), v.end()
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

typedef long long ll;
typedef pair<ll, ll> P;
char mp[55][55];
int n, m, bb, ww;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
void hoge(int h, int w) {
  queue<pair<int, int>> q;
  bool isb = false, isw = false;
  q.push(MP(h, w));
  int ret = 0;
  while (!q.empty()) {
    auto now = q.front();
    q.pop();
    mp[now.FI][now.SE] = '#';
    ret++;
    REP(i, 4) {
      int yy = now.FI + dy[i];
      int xx = now.SE + dx[i];
      if (xx < 0 || yy < 0 || xx >= m || yy >= n)
        continue;
      if (mp[yy][xx] == '.')
        q.push(MP(yy, xx));
      if (mp[yy][xx] == 'B')
        isb = true;
      if (mp[yy][xx] == 'W')
        isw = true;
    }
  }
  if ((isb & isw) || !(isb | isw))
    return;
  if (isb)
    bb += ret;
  else
    ww += ret;
}

int main() {
  while (cin >> n >> m, n | m) {
    bb = 0, ww = 0;
    REP(i, n) REP(j, m) cin >> mp[i][j];
    REP(i, n) REP(j, m) {
      if (mp[i][j] == '.')
        hoge(i, j);
    }
    cout << bb << ' ' << ww << endl;
  }
  return 0;
}