Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_set>
#include <vector>
// #include <bits/stdc++.h>

using namespace std;
typedef long long lolo;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef unsigned long long ulolo;

const int maxn = (int)1e5 + 5;
const lolo maxx = (lolo)10000001;
const int maxm = (int)2e3 + 5;
const int INF = 0x3f3f3f3f;
const lolo inf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;
const int MOD = (int)1e6 + 3;

template <class T> inline void read(T &x) {
  x = 0;
  T flag = 1;
  char c;
  do {
    c = (char)getchar();
    if (c == '-')
      break;
  } while (c < '0' || c > '9');
  if (c == '-') {
    c = '0', flag = -1;
  }
  do {
    x = x * 10 + c - '0';
    c = (char)getchar();
  } while (c >= '0' && c <= '9');
  x *= flag;
}
template <class T> inline void out(T x) {
  if (x > 9)
    out(x / 10);
  x = x % 10;
  putchar('0' + x);
}

char g[1005][1005];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
queue<pii> Q;
int main() {
  freopen("../in.txt", "r", stdin);
  int n, m;
  read(n);
  read(m);
  for (int i = 0; i < n; ++i) {
    scanf("%s", g[i]);
    for (int j = 0; j < m; ++j) {
      if (g[i][j] == '#') {
        Q.push(make_pair(i, j));
      }
    }
  }
  int ccnt = Q.size();
  int ncnt = 0, ans = 0;
  while (!Q.empty()) {
    pii x = Q.front();
    Q.pop();
    for (int i = 0; i < 4; ++i) {
      int nx = x.first + dx[i], ny = x.second + dy[i];
      if (nx < 0 || nx >= n || ny < 0 || ny >= m || g[nx][ny] == '#') {
        continue;
      }
      g[nx][ny] = '#';
      Q.push(make_pair(nx, ny));
    }
    if (++ncnt == ccnt) {
      ncnt = 0;
      ccnt = Q.size();
      ++ans;
    }
  }
  printf("%d\n", ans - 1);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_set>
#include <vector>
// #include <bits/stdc++.h>

using namespace std;
typedef long long lolo;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef unsigned long long ulolo;

const int maxn = (int)1e5 + 5;
const lolo maxx = (lolo)10000001;
const int maxm = (int)2e3 + 5;
const int INF = 0x3f3f3f3f;
const lolo inf = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-6;
const int MOD = (int)1e6 + 3;

template <class T> inline void read(T &x) {
  x = 0;
  T flag = 1;
  char c;
  do {
    c = (char)getchar();
    if (c == '-')
      break;
  } while (c < '0' || c > '9');
  if (c == '-') {
    c = '0', flag = -1;
  }
  do {
    x = x * 10 + c - '0';
    c = (char)getchar();
  } while (c >= '0' && c <= '9');
  x *= flag;
}
template <class T> inline void out(T x) {
  if (x > 9)
    out(x / 10);
  x = x % 10;
  putchar('0' + x);
}

char g[1005][1005];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
queue<pii> Q;
int main() {
  int n, m;
  read(n);
  read(m);
  for (int i = 0; i < n; ++i) {
    scanf("%s", g[i]);
    for (int j = 0; j < m; ++j) {
      if (g[i][j] == '#') {
        Q.push(make_pair(i, j));
      }
    }
  }
  int ccnt = Q.size();
  int ncnt = 0, ans = 0;
  while (!Q.empty()) {
    pii x = Q.front();
    Q.pop();
    for (int i = 0; i < 4; ++i) {
      int nx = x.first + dx[i], ny = x.second + dy[i];
      if (nx < 0 || nx >= n || ny < 0 || ny >= m || g[nx][ny] == '#') {
        continue;
      }
      g[nx][ny] = '#';
      Q.push(make_pair(nx, ny));
    }
    if (++ncnt == ccnt) {
      ncnt = 0;
      ccnt = Q.size();
      ++ans;
    }
  }
  printf("%d\n", ans - 1);
  return 0;
}