Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

#define CHMAX(a, b) a = std::max(a, b)
#define CHMIN(a, b) a = std::min(a, b)
#define CHABS(a) a = std::abs(a)
#define COUT(a) std::cout << a << std::endl
#define CERR(a) std::cerr << a << std::endl
#define FOR(n) for (lli i = 0; i < n; i++)

using namespace std;
using lli = long long int;
using pll = pair<lli, lli>;
using tlll = tuple<lli, lli, lli>;

lli mod197 = 1000000007LL;

// ax + by = gcd(a,b) 最大公約数
template <typename T> T extgcd(T a, T b, T &x, T &y) {
  T d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

lli visited[1002][1002] = {};

int main(void) {

  lli H, W;
  cin >> H >> W;

  std::queue<tlll> q;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char c;
      cin >> c;
      if (c == '#') {
        q.push(tlll{i + 1, j + 1, 1});
      }
    }
  }

  for (int i = 1; i <= H; i++) {
    visited[i][W + 1] = 1;
    visited[i][0] = 1;
  }
  for (int i = 1; i <= W; i++) {
    visited[0][i] = 1;
    visited[W + 1][i] = 1;
  }

  lli ans = 0;
  lli dir[] = {1, 0, -1, 0, 1};

  while (!q.empty()) {

    tlll t = q.front();
    q.pop();

    if (visited[get<0>(t)][get<1>(t)] == 0) {
      visited[get<0>(t)][get<1>(t)] = get<2>(t);
      ans = get<2>(t);
      for (int i = 0; i < 4; i++)
        q.push(tlll{get<0>(t) + dir[i], get<1>(t) + dir[i + 1], get<2>(t) + 1});
    }
  }

  COUT(ans - 1);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <vector>

#define CHMAX(a, b) a = std::max(a, b)
#define CHMIN(a, b) a = std::min(a, b)
#define CHABS(a) a = std::abs(a)
#define COUT(a) std::cout << a << std::endl
#define CERR(a) std::cerr << a << std::endl
#define FOR(n) for (lli i = 0; i < n; i++)

using namespace std;
using lli = long long int;
using pll = pair<lli, lli>;
using tlll = tuple<lli, lli, lli>;

lli mod197 = 1000000007LL;

// ax + by = gcd(a,b) 最大公約数
template <typename T> T extgcd(T a, T b, T &x, T &y) {
  T d = a;
  if (b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}

lli visited[1002][1002] = {};

int main(void) {

  lli H, W;
  cin >> H >> W;

  std::queue<tlll> q;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char c;
      cin >> c;
      if (c == '#') {
        q.push(tlll{i + 1, j + 1, 1});
      }
    }
  }

  for (int i = 1; i <= H; i++) {
    visited[i][W + 1] = 1;
    visited[i][0] = 1;
  }
  for (int i = 1; i <= W; i++) {
    visited[0][i] = 1;
    visited[H + 1][i] = 1;
  }

  lli ans = 0;
  lli dir[] = {1, 0, -1, 0, 1};

  while (!q.empty()) {

    tlll t = q.front();
    q.pop();

    if (visited[get<0>(t)][get<1>(t)] == 0) {
      visited[get<0>(t)][get<1>(t)] = get<2>(t);
      ans = get<2>(t);
      for (int i = 0; i < 4; i++)
        q.push(tlll{get<0>(t) + dir[i], get<1>(t) + dir[i + 1], get<2>(t) + 1});
    }
  }

  COUT(ans - 1);

  return 0;
}