Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}

template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Add(T &a, const T &b, const T &mod = 1000000007) {
  int val = ((a % mod) + (b % mod)) % mod;
  if (val < 0) {
    val += mod;
  }
  a = val;
}
////////////////////////////////////////
int H, W;
char A[1010][1010];
int d[1010][1010];
int main() {
  cin >> H >> W;
  queue<pii> Q;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> A[i][j];
      if (A[i][j] == '#') {
        Q.push({i, j});
        d[i][j] = 0;
      } else {
        d[i][j] = INF;
      }
    }
  }
  while (!Q.empty()) {
    int y = Q.front().first;
    int x = Q.front().second;
    Q.pop();
    for (int i = 0; i < 4; ++i) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (0 <= ny && ny < H && 0 <= nx && nx < W && A[ny][nx] == '.') {
        Q.push({ny, nx});
        d[ny][nx] = min(d[ny][nx], d[y][x] + 1);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (d[i][j] == 0)
        continue;
      ans = max(ans, d[i][j]);
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define REP(i, n) FOR(i, 0, n - 1)
#define NREP(i, n) FOR(i, 1, n)
using ll = long long;
using pii = pair<int, int>;
using piii = pair<pii, pii>;
const int dx[4] = {0, -1, 1, 0};
const int dy[4] = {-1, 0, 0, 1};
const int INF = 1e9 + 7;
int gcd(int x, int y) {
  if (x < y)
    swap(x, y);
  if (y == 0)
    return x;
  return gcd(y, x % y);
}

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}

template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <class T> void Add(T &a, const T &b, const T &mod = 1000000007) {
  int val = ((a % mod) + (b % mod)) % mod;
  if (val < 0) {
    val += mod;
  }
  a = val;
}
////////////////////////////////////////
int H, W;
char A[1010][1010];
int d[1010][1010];
int main() {
  cin >> H >> W;
  queue<pii> Q;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> A[i][j];
      if (A[i][j] == '#') {
        Q.push({i, j});
        d[i][j] = 0;
      } else {
        d[i][j] = INF;
      }
    }
  }
  while (!Q.empty()) {
    int y = Q.front().first;
    int x = Q.front().second;
    Q.pop();
    for (int i = 0; i < 4; ++i) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (0 <= ny && ny < H && 0 <= nx && nx < W && A[ny][nx] == '.' &&
          d[ny][nx] == INF) {
        Q.push({ny, nx});
        d[ny][nx] = min(d[ny][nx], d[y][x] + 1);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      if (d[i][j] == 0)
        continue;
      ans = max(ans, d[i][j]);
    }
  }
  cout << ans << endl;
  return 0;
}