Problem p03053 (C++)

Buggy submission (status: Runtime Error):
/**
 *    Author: "Robin Singh" !!
 *    rob_in_1
 **/

#include <bits/stdc++.h>
using namespace std;

#define dbg(args...)                                                           \
  {                                                                            \
    vector<string> _v = split(#args, ',');                                     \
    err(_v.begin(), args);                                                     \
    cerr << '\n';                                                              \
  }
vector<string> split(const string &s, char c) {
  vector<string> v;
  stringstream ss(s);
  string x;
  while (getline(ss, x, c))
    v.emplace_back(x);
  return move(v);
}
void err(vector<string>::iterator it) {}
template <typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
  cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << '\t';
  err(++it, args...);
}

#define int long long
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define MP make_pair
#define szz(v) ((int)(v).size())
#define fr(i, j, k) for (int i = j; i < k; i++)
#define bk(i, j, k) for (int i = j - 1; i >= k; i--)
#define mem(ptr, val) memset(ptr, val, sizeof ptr)
#define sbit(n) __builtin_popcount(n)
#define nl "\n"

typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpii;
const long double eps = 1e-9;
const int mod = 1e9 + 7;

pii dir[8] = {{-1, 0}, {1, 0},   {0, -1}, {0, 1},
              {1, 1},  {-1, -1}, {-1, 1}, {1, -1}};

//*********************************!Template Ends
//Here!**********************************//

const int nn = 1e3 + 5;
int dist[nn][nn];
string s[nn];
int h, w;
queue<pii> q;

void bfs() {
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int i = p.ff;
    int j = p.ss;

    for (int ii = 0; ii < 4; ii++) {
      int x = i + dir[ii].ff, y = j + dir[ii].ss;

      if (x >= 0 and y >= 0 and x < h and y < w and dist[x][y] == -1) {
        dist[x][y] = 1 + dist[i][j];
        q.push({x, y});
      }
    }
  }
}

void robin() {
  cin >> h >> w;
  fr(i, 0, h) cin >> s[i];
  int ans = 0;
  memset(dist, -1, sizeof dist);

  fr(i, 0, h) {
    fr(j, 0, w) {
      if (s[i][j] == '#') {
        dist[i][j] = 0;
        q.push({i, j});
      }
    }
  }
  bfs();
  fr(i, 0, h) fr(j, 0, w) ans = max(ans, dist[i][j]);
  cout << ans;
}

int32_t main() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  // cin >> T;
  for (int tc = 1; tc <= T; tc++) {
    // cout << "Case #" << tc << ": ";
    robin();
  }
  return 0;
}

Fixed submission:
/**
 *    Author: "Robin Singh" !!
 *    rob_in_1
 **/

#include <bits/stdc++.h>
using namespace std;

#define dbg(args...)                                                           \
  {                                                                            \
    vector<string> _v = split(#args, ',');                                     \
    err(_v.begin(), args);                                                     \
    cerr << '\n';                                                              \
  }
vector<string> split(const string &s, char c) {
  vector<string> v;
  stringstream ss(s);
  string x;
  while (getline(ss, x, c))
    v.emplace_back(x);
  return move(v);
}
void err(vector<string>::iterator it) {}
template <typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
  cerr << it->substr((*it)[0] == ' ', it->length()) << " = " << a << '\t';
  err(++it, args...);
}

#define int long long
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define ff first
#define ss second
#define MP make_pair
#define szz(v) ((int)(v).size())
#define fr(i, j, k) for (int i = j; i < k; i++)
#define bk(i, j, k) for (int i = j - 1; i >= k; i--)
#define mem(ptr, val) memset(ptr, val, sizeof ptr)
#define sbit(n) __builtin_popcount(n)
#define nl "\n"

typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vpii;
const long double eps = 1e-9;
const int mod = 1e9 + 7;

pii dir[8] = {{-1, 0}, {1, 0},   {0, -1}, {0, 1},
              {1, 1},  {-1, -1}, {-1, 1}, {1, -1}};

//*********************************!Template Ends
//Here!**********************************//

const int nn = 1e3 + 5;
int dist[nn][nn];
string s[nn];
int h, w;
queue<pii> q;

void bfs() {
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int i = p.ff;
    int j = p.ss;

    for (int ii = 0; ii < 4; ii++) {
      int x = i + dir[ii].ff, y = j + dir[ii].ss;

      if (x >= 0 and y >= 0 and x < h and y < w and dist[x][y] == -1) {
        dist[x][y] = 1 + dist[i][j];
        q.push({x, y});
      }
    }
  }
}

void robin() {
  cin >> h >> w;
  fr(i, 0, h) cin >> s[i];
  int ans = 0;
  memset(dist, -1, sizeof dist);

  fr(i, 0, h) {
    fr(j, 0, w) {
      if (s[i][j] == '#') {
        dist[i][j] = 0;
        q.push({i, j});
      }
    }
  }
  bfs();
  fr(i, 0, h) fr(j, 0, w) ans = max(ans, dist[i][j]);
  cout << ans;
}

int32_t main() {
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T = 1;
  // cin >> T;
  for (int tc = 1; tc <= T; tc++) {
    // cout << "Case #" << tc << ": ";
    robin();
  }
  return 0;
}