Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;
const int MOD = 1000000007;

signed main() {

  int h, w;
  cin >> h >> w;
  queue<pair<int, int>> q;
  vector<vector<int>> g(h, vector<int>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char in;
      cin >> in;
      if (in == '#') {
        g[i][j] = 1;
        q.push({i, j});
      } else
        g[i][j] = 0;
    }
  }

  int di[4] = {0, 1, 0, -1};
  int dj[4] = {1, 0, -1, 0};
  int cnt = 0;
  while (1) {
    vector<pair<int, int>> nxt;
    if (q.empty()) {
      cout << cnt - 1 << endl;
      return 0;
    }

    while (!q.empty()) {
      int i = q.front().first;
      int j = q.front().second;
      cerr << i << " " << j << endl;
      q.pop();

      for (int l = 0; l < 4; l++) {
        int ni = i + di[l];
        int nj = j + dj[l];
        if (ni < 0 || h <= ni || nj < 0 || w <= nj)
          continue;
        if (g[ni][nj] == 1)
          continue;

        g[ni][nj] = 1;
        nxt.push_back({ni, nj});
      }
    }

    for (int i = 0; i < (int)nxt.size(); i++)
      q.push(nxt[i]);
    cnt++;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;
const int MOD = 1000000007;

signed main() {

  int h, w;
  cin >> h >> w;
  queue<pair<int, int>> q;
  vector<vector<int>> g(h, vector<int>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char in;
      cin >> in;
      if (in == '#') {
        g[i][j] = 1;
        q.push({i, j});
      } else
        g[i][j] = 0;
    }
  }

  int di[4] = {0, 1, 0, -1};
  int dj[4] = {1, 0, -1, 0};
  int cnt = 0;
  while (1) {
    vector<pair<int, int>> nxt;
    if (q.empty()) {
      cout << cnt - 1 << endl;
      return 0;
    }

    while (!q.empty()) {
      int i = q.front().first;
      int j = q.front().second;
      q.pop();

      for (int l = 0; l < 4; l++) {
        int ni = i + di[l];
        int nj = j + dj[l];
        if (ni < 0 || h <= ni || nj < 0 || w <= nj)
          continue;
        if (g[ni][nj] == 1)
          continue;

        g[ni][nj] = 1;
        nxt.push_back({ni, nj});
      }
    }

    for (int i = 0; i < (int)nxt.size(); i++)
      q.push(nxt[i]);
    cnt++;
  }
  return 0;
}