prithivMLmods/Coder-Stat huggingface dataset:
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define dunk(a) cout << (a) << endl
#define rall(a) (a).rbegin(), (a).rend()
const int INF = 2e9;
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
typedef long long ll;

char G[55][55];
int dist[55][55];
queue<pair<int, int>> que;
// North, west, south, east
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, -1, 0, 1};

int main() {
  int h, w;
  cin >> h >> w;
  int ans = 0;

  rep(i, h) {
    rep(j, w) {
      cin >> G[i][j];
      dist[i][j] = -1;
      if (G[i][j] == '#') {
        que.push(P(i, j));
        dist[i][j] = 0;
      }
    }
  }

  while (!que.empty()) {
    P p = que.front();
    que.pop();

    rep(i, 4) {
      int ny = p.first + dy[i];
      int nx = p.second + dx[i];

      if (-1 < ny && ny < h && -1 < nx && nx < w && G[ny][nx] == '.' &&
          dist[ny][nx] == -1) {
        dist[ny][nx] = dist[p.first][p.second] + 1;
        que.push(P(ny, nx));
      }
    }
  }

  rep(i, h) {
    rep(j, w) { ans = max(ans, dist[i][j]); }
  }

  dunk(ans);
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define dunk(a) cout << (a) << endl
#define rall(a) (a).rbegin(), (a).rend()
const int INF = 2e9;
using namespace std;
using Graph = vector<vector<int>>;
typedef pair<int, int> P;
typedef long long ll;

char G[1100][1100];
int dist[1100][1100];
queue<pair<int, int>> que;
// North, west, south, east
int dy[] = {-1, 0, 1, 0};
int dx[] = {0, -1, 0, 1};

int main() {
  int h, w;
  cin >> h >> w;
  int ans = 0;

  rep(i, h) {
    rep(j, w) {
      cin >> G[i][j];
      dist[i][j] = -1;
      if (G[i][j] == '#') {
        que.push(P(i, j));
        dist[i][j] = 0;
      }
    }
  }

  while (!que.empty()) {
    P p = que.front();
    que.pop();

    rep(i, 4) {
      int ny = p.first + dy[i];
      int nx = p.second + dx[i];

      if (-1 < ny && ny < h && -1 < nx && nx < w && G[ny][nx] == '.' &&
          dist[ny][nx] == -1) {
        dist[ny][nx] = dist[p.first][p.second] + 1;
        que.push(P(ny, nx));
      }
    }
  }

  rep(i, h) {
    rep(j, w) { ans = max(ans, dist[i][j]); }
  }

  dunk(ans);
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
char s[1005][1005];
int d[1005][1005];
int x[1005], y[1005];
int h, w;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
int main() {
  scanf("%d%d", &h, &w);
  getchar();
  int t = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      scanf("%c", &s[i][j]);
      if (s[i][j] == '.')
        d[i][j] = 1e9;
      else {
        d[i][j] = 0;
        x[t] = i;
        y[t++] = j;
      }
    }
    getchar();
  }
  int s = 0, ans = 0;
  while (s < t) {
    int xx = x[s], yy = y[s++];
    for (int i = 0; i < 4; i++) {
      int u = xx + dx[i];
      int v = yy + dy[i];
      if (u < 1 || u > h || v < 1 || v > w || d[u][v] <= d[xx][yy] + 1)
        continue;
      d[u][v] = d[xx][yy] + 1;
      ans = max(ans, d[u][v]);
      x[t] = u;
      y[t++] = v;
    }
  }
  printf("%d\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
char s[1005][1005];
int d[1005][1005];
int x[1000005], y[1000005];
int h, w;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
int main() {
  scanf("%d%d", &h, &w);
  getchar();
  int t = 0;
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      scanf("%c", &s[i][j]);
      if (s[i][j] == '.')
        d[i][j] = 1e9;
      else {
        d[i][j] = 0;
        x[t] = i;
        y[t++] = j;
      }
    }
    getchar();
  }
  int s = 0, ans = 0;
  while (s < t) {
    int xx = x[s], yy = y[s++];
    for (int i = 0; i < 4; i++) {
      int u = xx + dx[i];
      int v = yy + dy[i];
      if (u < 1 || u > h || v < 1 || v > w || d[u][v] <= d[xx][yy] + 1)
        continue;
      d[u][v] = d[xx][yy] + 1;
      ans = max(ans, d[u][v]);
      x[t] = u;
      y[t++] = v;
    }
  }
  printf("%d\n", ans);
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
const int MOD = 1000000007;
const int INF = numeric_limits<int>::max() / 2;

typedef pair<int, int> P;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int H, W;
  cin >> H >> W;
  vector<string> s(H);
  for (int i = 0; i < H; ++i) {
    cin >> s[i];
  }

  int dist[51][51];
  fill(dist[0], dist[H], INF);
  queue<P> que;
  rep(i, H) {
    rep(j, W) {
      if (s[i][j] == '#') {
        dist[i][j] = 0;
        que.push(P(i, j));
      }
    }
  }

  int ans = 0;
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int x = p.first;
    int y = p.second;

    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= H || ny < 0 || ny >= W)
        continue;
      if (s[nx][ny] == '#')
        continue;

      if (dist[nx][ny] > dist[x][y] + 1) {
        dist[nx][ny] = dist[x][y] + 1;
        que.push(P(nx, ny));
        ans = max(ans, dist[nx][ny]);
        s[nx][ny] = '#';
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
const int MOD = 1000000007;
const int INF = numeric_limits<int>::max() / 2;

typedef pair<int, int> P;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int H, W;
  cin >> H >> W;
  vector<string> s(H);
  for (int i = 0; i < H; ++i) {
    cin >> s[i];
  }

  int dist[H][W];
  fill(dist[0], dist[H], INF);
  queue<P> que;
  rep(i, H) {
    rep(j, W) {
      if (s[i][j] == '#') {
        dist[i][j] = 0;
        que.push(P(i, j));
      }
    }
  }

  int ans = 0;
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    int x = p.first;
    int y = p.second;

    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= H || ny < 0 || ny >= W)
        continue;
      if (s[nx][ny] == '#')
        continue;

      if (dist[nx][ny] > dist[x][y] + 1) {
        dist[nx][ny] = dist[x][y] + 1;
        que.push(P(nx, ny));
        ans = max(ans, dist[nx][ny]);
        s[nx][ny] = '#';
      }
    }
  }
  cout << ans << endl;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define endl '\n'

typedef long long ll;

struct S {
  int x, y, num;
};

char in[105][105];
int N, M, A[105][105], ans;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
queue<S> q;

bool safe(int x, int y) {
  if (0 < x && x <= N && 0 < y && y <= M)
    return 1;
  return 0;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  for (int i = 1; i <= N; i++)
    cin >> in[i] + 1;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      if (in[i][j] == '#') {
        A[i][j] = 1;
        q.push({i, j, 0});
      }
    }
  }
  while (!q.empty()) {
    int x = q.front().x, y = q.front().y, num = q.front().num;
    q.pop();
    ans = max(ans, num);
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (A[nx][ny] == 0 && safe(nx, ny)) {
        A[nx][ny] = 1;
        q.push({nx, ny, num + 1});
      }
    }
  }
  cout << ans;
}


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

#define endl '\n'

typedef long long ll;

struct S {
  int x, y, num;
};

char in[1005][1005];
int N, M, A[1005][1005], ans;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
queue<S> q;

bool safe(int x, int y) {
  if (0 < x && x <= N && 0 < y && y <= M)
    return 1;
  return 0;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  for (int i = 1; i <= N; i++)
    cin >> in[i] + 1;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      if (in[i][j] == '#') {
        A[i][j] = 1;
        q.push({i, j, 0});
      }
    }
  }
  while (!q.empty()) {
    int x = q.front().x, y = q.front().y, num = q.front().num;
    q.pop();
    ans = max(ans, num);
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (A[nx][ny] == 0 && safe(nx, ny)) {
        A[nx][ny] = 1;
        q.push({nx, ny, num + 1});
      }
    }
  }
  cout << ans;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> Masu(H, vector<char>(W));
  vector<vector<int>> Map(H, vector<int>(W, -1));
  deque<int> queY;
  deque<int> queX;
  getchar();
  for (int i = 0; i < W; i++) {
    for (int j = 0; j < H; j++) {
      Masu[i][j] = getchar();
      if (Masu[i][j] == '#') {
        queY.push_back(i);
        queX.push_back(j);
        Map[i][j] = 0;
      }
    }
    getchar();
  }
  int dy[4] = {1, 0, -1, 0};
  int dx[4] = {0, 1, 0, -1};
  int ans = 0;

  while (!queY.empty()) {
    int y = queY.front(), x = queX.front();
    queY.pop_front();
    queX.pop_front();
    int cnt = Map[y][x] + 1;
    for (int i = 0; i < 4; i++) {
      int newy = y + dy[i], newx = x + dx[i];
      if (newy < 0 || newy >= H || newx < 0 || newx >= W)
        continue;
      else if (Map[newy][newx] == -1) {
        queY.push_back(newy);
        queX.push_back(newx);
        Map[newy][newx] = cnt;
        ans = max(ans, cnt);
      }
    }
  }
  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> Masu(H, vector<char>(W));
  vector<vector<int>> Map(H, vector<int>(W, -1));
  deque<int> queY;
  deque<int> queX;
  getchar();
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      Masu[i][j] = getchar();
      if (Masu[i][j] == '#') {
        queY.push_back(i);
        queX.push_back(j);
        Map[i][j] = 0;
      }
    }
    getchar();
  }
  int dy[4] = {1, 0, -1, 0};
  int dx[4] = {0, 1, 0, -1};
  int ans = 0;

  while (!queY.empty()) {
    int y = queY.front(), x = queX.front();
    queY.pop_front();
    queX.pop_front();
    int cnt = Map[y][x] + 1;
    for (int i = 0; i < 4; i++) {
      int newy = y + dy[i], newx = x + dx[i];
      if (newy < 0 || newy >= H || newx < 0 || newx >= W)
        continue;
      else if (Map[newy][newx] == -1) {
        queY.push_back(newy);
        queX.push_back(newx);
        Map[newy][newx] = cnt;
        ans = max(ans, cnt);
      }
    }
  }
  cout << ans << "\n";
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>

using namespace std;

typedef pair<int, int> P;

int DYX[4][2]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

int H, W;
int ans = 0;
string graph[50];
int seen[50][50];
queue<P> que;

void bfs();

int main() {
  cin >> H >> W;
  for (int h = 0; h < H; h++) {
    cin >> graph[h];
    for (int w = 0; w < W; w++) {
      seen[h][w] = -1;
      if (graph[h][w] == '#') {
        que.push(P(h, w));
        seen[h][w] = 0;
      }
    }
  }
  bfs();
  cout << ans << "\n";
  return 0;
}
void bfs() {
  while (que.size()) {
    P next = que.front();
    que.pop();
    int y = next.first, x = next.second;
    for (auto dyx : DYX) {
      int ny = y + dyx[0], nx = x + dyx[1];
      if (0 <= ny && ny < H && 0 <= nx && nx < W && graph[ny][nx] != '#' &&
          seen[ny][nx] == -1) {
        seen[ny][nx] = seen[y][x] + 1;
        ans = max(seen[ny][nx], ans);
        que.push(P(ny, nx));
      }
    }
  }
  return;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>

using namespace std;

typedef pair<int, int> P;

int DYX[4][2]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

int H, W;
int ans = 0;
string graph[1000];
int seen[1000][1000];
queue<P> que;

void bfs();

int main() {
  cin >> H >> W;
  for (int h = 0; h < H; h++) {
    cin >> graph[h];
    for (int w = 0; w < W; w++) {
      seen[h][w] = -1;
      if (graph[h][w] == '#') {
        que.push(P(h, w));
        seen[h][w] = 0;
      }
    }
  }
  bfs();
  cout << ans << "\n";
  return 0;
}
void bfs() {
  while (que.size()) {
    P next = que.front();
    que.pop();
    int y = next.first, x = next.second;
    for (auto dyx : DYX) {
      int ny = y + dyx[0], nx = x + dyx[1];
      if (0 <= ny && ny < H && 0 <= nx && nx < W && graph[ny][nx] != '#' &&
          seen[ny][nx] == -1) {
        seen[ny][nx] = seen[y][x] + 1;
        ans = max(seen[ny][nx], ans);
        que.push(P(ny, nx));
      }
    }
  }
  return;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

#define ran 1011

const int go[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};

int n, m;
char s[ran][ran];
int d[ran][ran];
int q[ran * ran], l, r;

int main() {
  scanf("%d%d", &n, &m);
  memset(d, -1, sizeof d);
  l = r = 0;
  for (int i = 0; i < n; i++) {
    scanf("%s", s[i]);
    for (int j = 0; j < m; j++)
      if (s[i][j] == '#') {
        q[r++] = i;
        q[r++] = j;
        d[i][j] = 0;
      }
  }

  int ans = 0;
  while (l < r) {
    int cur_x = q[l++], cur_y = q[l++];
    int cur_d = d[cur_x][cur_y];
    for (int i = 0; i < 4; i++) {
      int x = cur_x + go[i][0], y = cur_y + go[i][1];
      if (x < 0 || x >= n || y < 0 || y >= m)
        continue;
      if (d[x][y] >= 0)
        continue;

      d[x][y] = cur_d + 1;
      q[r++] = x;
      q[r++] = y;
      ans = max(ans, d[x][y]);
    }
  }
  printf("%d\n", ans);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;

#define ran 1011

const int go[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};

int n, m;
char s[ran][ran];
int d[ran][ran];
int q[ran * ran * 2], l, r;

int main() {
  scanf("%d%d", &n, &m);
  memset(d, -1, sizeof d);
  l = r = 0;
  for (int i = 0; i < n; i++) {
    scanf("%s", s[i]);
    for (int j = 0; j < m; j++)
      if (s[i][j] == '#') {
        q[r++] = i;
        q[r++] = j;
        d[i][j] = 0;
      }
  }

  int ans = 0;
  while (l < r) {
    int cur_x = q[l++], cur_y = q[l++];
    int cur_d = d[cur_x][cur_y];
    for (int i = 0; i < 4; i++) {
      int x = cur_x + go[i][0], y = cur_y + go[i][1];
      if (x < 0 || x >= n || y < 0 || y >= m)
        continue;
      if (d[x][y] >= 0)
        continue;

      d[x][y] = cur_d + 1;
      q[r++] = x;
      q[r++] = y;
      ans = max(ans, d[x][y]);
    }
  }
  printf("%d\n", ans);

  return 0;
}
Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int h, w;
vector<string> a;

inline bool can(int x, int y, vector<vector<int>> dist) {
  return x >= 0 && x < h && y >= 0 && y < w && dist[x][y] == -1;
}

int vectormax(vector<vector<int>> dist) {
  int ans = 0;
  for (int i = 0; i < dist.size(); i++) {
    ans = max(ans, *max_element(dist[i].begin(), dist[i].end()));
  }
  return ans;
}

int main() {
  cin >> h >> w;
  a.resize(h);
  for (int i = 0; i < h; i++) {
    cin >> a[i];
  }
  queue<pair<int, int>> que;
  vector<vector<int>> dist(h, vector<int>(w, -1));

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == '#') {
        dist[i][j] = 0;
        que.push(make_pair(i, j));
      }
    }
  }

  while (!que.empty()) {
    pair<int, int> temp = que.front();
    que.pop();

    int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

    for (int i = 0; i < 4; i++) {
      int x, y;
      x = temp.first + dx[i];
      y = temp.second + dy[i];
      if (can(x, y, dist)) {
        dist[x][y] = dist[temp.first][temp.second] + 1;
        que.emplace(make_pair(x, y));
      }
    }
  }

  cout << vectormax(dist) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int h, w;
vector<string> a;

bool can(int x, int y, vector<vector<int>> &dist) {
  return x >= 0 && x < h && y >= 0 && y < w && dist[x][y] == -1;
}

int vectormax(vector<vector<int>> dist) {
  int ans = 0;
  for (int i = 0; i < dist.size(); i++) {
    ans = max(ans, *max_element(dist[i].begin(), dist[i].end()));
  }
  return ans;
}

int main() {
  cin >> h >> w;
  a.resize(h);
  for (int i = 0; i < h; i++) {
    cin >> a[i];
  }
  queue<pair<int, int>> que;
  vector<vector<int>> dist(h, vector<int>(w, -1));

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (a[i][j] == '#') {
        dist[i][j] = 0;
        que.push(make_pair(i, j));
      }
    }
  }

  while (!que.empty()) {
    pair<int, int> temp = que.front();
    que.pop();

    int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

    for (int i = 0; i < 4; i++) {
      int x, y;
      x = temp.first + dx[i];
      y = temp.second + dy[i];
      if (can(x, y, dist)) {
        dist[x][y] = dist[temp.first][temp.second] + 1;
        que.emplace(make_pair(x, y));
      }
    }
  }

  cout << vectormax(dist) << endl;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
// #define MOD 1000000007
const int INF = 1LL << 30;

char fi[1010][1010];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int dist[1010][1010];

int main() {
  int h, w;
  cin >> h >> w;

  rep(i, h) rep(j, w) dist[i][j] = -1;
  queue<pair<int, int>> que;
  rep(i, h) {
    rep(j, w) {
      cin >> fi[i][j];
      if (fi[i][j] == '#') {
        que.push({i, j});
        dist[i][j] = 0;
      }
    }
  }

  while (!que.empty()) {
    auto v = que.front(); // キューから先頭頂点を取り出す
    que.pop();

    int x = v.first;
    int y = v.second;

    rep(i, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (dist[nx][ny] != -1)
        continue;
      dist[nx][ny] = dist[x][y] + 1;
      que.push({nx, ny});
    }
  }

  int ans = 0;
  rep(i, h) {
    rep(j, w) { ans = max(ans, dist[i][j]); }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
// #define MOD 1000000007
const int INF = 1LL << 30;

char fi[1010][1010];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int dist[1010][1010];

int main() {
  int h, w;
  cin >> h >> w;

  rep(i, h) rep(j, w) dist[i][j] = -1;
  queue<pair<int, int>> que;
  rep(i, h) {
    rep(j, w) {
      cin >> fi[i][j];
      if (fi[i][j] == '#') {
        que.push({i, j});
        dist[i][j] = 0;
      }
    }
  }

  while (!que.empty()) {
    auto v = que.front(); // キューから先頭頂点を取り出す
    que.pop();

    int x = v.first;
    int y = v.second;

    rep(i, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (dist[nx][ny] != -1)
        continue;
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      dist[nx][ny] = dist[x][y] + 1;
      que.push({nx, ny});
    }
  }

  int ans = 0;
  rep(i, h) {
    rep(j, w) { ans = max(ans, dist[i][j]); }
  }

  cout << ans << endl;
}
Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int H, W;
  cin >> H >> W;

  vector<vector<bool>> field(H, vector<bool>(W, false));
  string A;
  int count = 0;
  queue<pair<int, int>> q_new;
  queue<pair<int, int>> q_old;
  for (int i = 0; i < H; i++) {
    cin >> A;
    for (int j = 0; j < W; j++) {
      if (A[j] == '#') {
        field[i][j] = true;
        q_new.push(make_pair(i, j));
        count++;
      } else {
        field[i][j] = false;
      }
    }
  }

  int ans = 0;
  int diff[4][2] = {
      {0, 1},
      {0, -1},
      {1, 0},
      {-1, 0},
  };

  while (count < H * W) {
    ans++;
    q_old = q_new;
    while (!q_old.empty()) {
      int x_old = q_old.front().first;
      int y_old = q_old.front().second;
      q_old.pop();
      for (int i = 0; i < 4; i++) {
        int x_new = x_old + diff[i][0];
        int y_new = y_old + diff[i][1];
        if (x_new < 0 || H <= x_new) {
          continue;
        }
        if (y_new < 0 || W <= y_new) {
          continue;
        }
        if (field[x_new][y_new]) {
          continue;
        }
        field[x_new][y_new] = true;
        count++;
        q_new.push(make_pair(x_new, y_new));
      }
    }
  }
  cout << ans << endl;
}


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

int main() {
  int H, W;
  cin >> H >> W;

  vector<vector<bool>> field(H, vector<bool>(W, false));
  string A;
  int count = 0;
  queue<pair<int, int>> q_new;
  queue<pair<int, int>> q_old;
  for (int i = 0; i < H; i++) {
    cin >> A;
    for (int j = 0; j < W; j++) {
      if (A[j] == '#') {
        field[i][j] = true;
        q_new.push(make_pair(i, j));
        count++;
      } else {
        field[i][j] = false;
      }
    }
  }

  int ans = 0;
  int diff[4][2] = {
      {0, 1},
      {0, -1},
      {1, 0},
      {-1, 0},
  };

  while (count < H * W) {
    ans++;
    q_old = q_new;
    q_new = queue<pair<int, int>>();
    while (!q_old.empty()) {
      int x_old = q_old.front().first;
      int y_old = q_old.front().second;
      q_old.pop();
      for (int i = 0; i < 4; i++) {
        int x_new = x_old + diff[i][0];
        int y_new = y_old + diff[i][1];
        if (x_new < 0 || H <= x_new) {
          continue;
        }
        if (y_new < 0 || W <= y_new) {
          continue;
        }
        if (field[x_new][y_new]) {
          continue;
        }
        field[x_new][y_new] = true;
        count++;
        q_new.push(make_pair(x_new, y_new));
      }
    }
  }
  cout << ans << endl;
}
Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define INF 1000000000
#define MOD 1000000007
#define ll long long int

using namespace std;

int main() {
  int H, W;
  cin >> H >> W;
  int time[1009][1009];
  for (int i = 0; i < 1009; i++) {
    for (int j = 0; j < 1009; j++) {
      time[i][j] = INF;
    }
  }

  queue<int> qx;
  queue<int> qy;

  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      char c;
      cin >> c;
      if (c == '#') {
        time[i][j] = 0;
        qx.push(i);
        qy.push(j);
      }
    }
  }

  while (!qx.empty()) {
    int nowx = qx.front();
    qx.pop();
    int nowy = qy.front();
    qy.pop();
    vector<int> dx = {1, 0, -1, 0};
    vector<int> dy = {0, 1, 0, -1};
    for (int i = 0; i < 4; i++) {
      int x = nowx + dx[i];
      int y = nowy + dy[i];
      if (x > H or x == 0 or y == W + 1 or y == 0)
        continue;
      time[x][y] = min(time[x][y], time[nowx][nowy] + 1);
      if (time[x][y] == time[nowx][nowy] + 1) {
        qx.push(x);
        qy.push(y);
      }
    }
  }

  int ans = -1;

  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {

      ans = max(ans, time[i][j]);
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define INF 1000000000
#define MOD 1000000007
#define ll long long int

using namespace std;

int main() {
  int H, W;
  cin >> H >> W;
  int time[1009][1009];
  for (int i = 0; i < 1009; i++) {
    for (int j = 0; j < 1009; j++) {
      time[i][j] = INF;
    }
  }

  queue<int> qx;
  queue<int> qy;

  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      char c;
      cin >> c;
      if (c == '#') {
        time[i][j] = 0;
        qx.push(i);
        qy.push(j);
      }
    }
  }

  while (!qx.empty()) {
    int nowx = qx.front();
    qx.pop();
    int nowy = qy.front();
    qy.pop();
    vector<int> dx = {1, 0, -1, 0};
    vector<int> dy = {0, 1, 0, -1};
    for (int i = 0; i < 4; i++) {
      int x = nowx + dx[i];
      int y = nowy + dy[i];
      if (x > H or x == 0 or y == W + 1 or y == 0)
        continue;
      if (time[x][y] <= time[nowx][nowy] + 1)
        continue;
      time[x][y] = time[nowx][nowy] + 1;

      // time[x][y] = min(time[x][y],time[nowx][nowy] + 1);
      // if(time[x][y] == time[nowx][nowy] + 1) {
      qx.push(x);
      qy.push(y);
      //}
    }
  }

  int ans = -1;

  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {

      ans = max(ans, time[i][j]);
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;

typedef long long ll;
typedef long double ld;

#define REP(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++)
#define rep(i, n) REP(i, 0, n)
#define vec vector

const ll large_P = 1e9 + 7;

int main() {
  int H, W;
  cin >> H >> W;

  vector<string> A(H);
  rep(i, H) cin >> A.at(i);

  vector<vector<int>> dist(H, vector<int>(W, -1));
  queue<pair<int, int>> que;

  rep(i, H) {
    rep(j, W) {
      if (A.at(i).at(j) == '#') {
        dist.at(i).at(j) = 0;
        que.push(make_pair(i, j));
      } else {
        continue;
      }
    }
  }

  while (!que.empty()) {
    pair<int, int> v = que.front();
    que.pop();

    for (int k = -1; k <= 1; k++) {
      for (int l = -1; l <= 1; l++) {
        if (k * l != 0)
          continue;
        if (v.first + k < 0 || H <= v.first + k)
          continue;
        if (v.second + l < 0 || W <= v.second + l)
          continue; // 場外
        if (dist.at(v.first + k).at(v.second + l) <
                dist.at(v.first).at(v.second) + 1 &&
            dist.at(v.first + k).at(v.second + l) != -1)
          continue;

        dist.at(v.first + k).at(v.second + l) =
            dist.at(v.first).at(v.second) + 1;
        que.push(make_pair(v.first + k, v.second + l));
      }
    }
  }

  int res = 0;
  rep(i, H) {
    rep(j, W) {
      if (res < dist.at(i).at(j))
        res = dist.at(i).at(j);
    }
  }

  cout << res;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;

typedef long long ll;
typedef long double ld;

#define REP(i, a, n) for (int(i) = (a); (i) < (int)(n); (i)++)
#define rep(i, n) REP(i, 0, n)
#define vec vector

const ll large_P = 1e9 + 7;

int main() {
  int H, W;
  cin >> H >> W;

  vector<string> A(H);
  rep(i, H) cin >> A.at(i);

  vector<vector<int>> dist(H, vector<int>(W, -1));
  queue<pair<int, int>> que;

  rep(i, H) {
    rep(j, W) {
      if (A.at(i).at(j) == '#') {
        dist.at(i).at(j) = 0;
        que.push(make_pair(i, j));
      } else {
        continue;
      }
    }
  }

  while (!que.empty()) {
    pair<int, int> v = que.front();
    que.pop();

    for (int k = -1; k <= 1; k++) {
      for (int l = -1; l <= 1; l++) {
        if (k * l != 0)
          continue;
        if (v.first + k < 0 || H <= v.first + k)
          continue;
        if (v.second + l < 0 || W <= v.second + l)
          continue; // 場外
        if (dist.at(v.first + k).at(v.second + l) != -1)
          continue;

        dist.at(v.first + k).at(v.second + l) =
            dist.at(v.first).at(v.second) + 1;
        que.push(make_pair(v.first + k, v.second + l));
      }
    }
  }

  int res = 0;
  rep(i, H) {
    rep(j, W) {
      if (res < dist.at(i).at(j))
        res = dist.at(i).at(j);
    }
  }

  cout << res;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define pb push_back
#define mp make_pair
#define ii pair<int, int>
#define ll long long
#define forn(i, a, b) for (int i = (int)a; i < (int)b; i++)
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w;
  cin >> h >> w;
  vector<vector<int>> score(h, vector<int>(w));
  queue<ii> q;
  forn(i, 0, h) {
    forn(j, 0, w) {
      char x;
      cin >> x;
      if (x == '#') {
        q.push(mp(i, j));
        score[i][j] = 0;
      } else {
        score[i][j] = -1;
      }
    }
  }

  int depth = 0;
  int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

  while (!q.empty()) {
    ii top = q.front();
    int s = score[top.first][top.second];
    q.pop();
    forn(i, 0, 4) {
      int nx = top.first + dir[i][0];
      int ny = top.second + dir[i][1];
      ii ntop = mp(nx, ny);
      if (nx > 0 && ny > 0 && nx < w - 1 && ny < h - 1 && score[nx][ny] < 0) {
        q.push(ntop);
        score[nx][ny] = s + 1;
        depth = max(depth, s + 1);
      }
    }
  }
  cout << depth << endl;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define pb push_back
#define mp make_pair
#define ii pair<int, int>
#define ll long long
#define forn(i, a, b) for (int i = (int)a; i < (int)b; i++)
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w;
  cin >> h >> w;
  vector<vector<int>> score(h, vector<int>(w));
  queue<ii> q;
  forn(i, 0, h) {
    forn(j, 0, w) {
      char x;
      cin >> x;
      if (x == '#') {
        q.push(mp(i, j));
        score[i][j] = 0;
      } else {
        score[i][j] = -1;
      }
    }
  }

  int depth = 0;
  int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

  while (!q.empty()) {
    ii top = q.front();
    int s = score[top.first][top.second];
    q.pop();
    forn(i, 0, 4) {
      int nx = top.first + dir[i][0];
      int ny = top.second + dir[i][1];
      ii ntop = mp(nx, ny);
      if (nx >= 0 && ny >= 0 && nx < h && ny < w && score[nx][ny] < 0) {
        q.push(ntop);
        score[nx][ny] = s + 1;
        depth = max(depth, s + 1);
      }
    }
  }
  cout << depth << endl;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long LL;
using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<char>> g(h, vector<char>(w));
  vector<vector<int>> d(h, vector<int>(w, -1));
  queue<pair<int, int>> Q;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> g[i][j];
      if (g[i][j] == '#') {
        d[i][j] = 0;
        Q.push(make_pair(i, j));
      }
    }
  }

  int ans = 0;
  int dx[4] = {1, -1, 0, 0};
  int dy[4] = {0, 0, 1, -1};
  pair<int, int> p;
  while (!Q.empty()) {
    p = Q.front();
    Q.pop();
    int x = p.first;
    int y = p.second;
    ans = d[x][y];
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (d[nx][ny] == -1 && nx >= 0 && nx < h && ny >= 0 && ny < w) {
        d[nx][ny] = d[x][y] + 1;
        Q.push(make_pair(nx, ny));
      }
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long LL;
using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<char>> g(h, vector<char>(w));
  vector<vector<int>> d(h, vector<int>(w, -1));
  queue<pair<int, int>> Q;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> g[i][j];
      if (g[i][j] == '#') {
        d[i][j] = 0;
        Q.push(make_pair(i, j));
      }
    }
  }

  int ans = 0;
  int dx[4] = {1, -1, 0, 0};
  int dy[4] = {0, 0, 1, -1};
  pair<int, int> p;
  while (!Q.empty()) {
    p = Q.front();
    Q.pop();
    int x = p.first;
    int y = p.second;
    ans = d[x][y];
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx >= 0 && nx < h && ny >= 0 && ny < w && d[nx][ny] == -1) {
        d[nx][ny] = d[x][y] + 1;
        Q.push(make_pair(nx, ny));
      }
    }
  }

  cout << ans << endl;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

int main() {
  typedef pair<char, char> pair;
  int H, W;
  cin >> H >> W;
  vector<string> maze(H);
  vector<vector<int>> dist(H, vector<int>(W, -1));
  for (int i = 0; i < H; i++)
    cin >> maze[i];

  deque<pair> q;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (maze[i][j] == '.')
        continue;
      q.push_back(make_pair(i, j));
      dist[i][j] = 0;
    }
  }

  while (!q.empty()) {
    auto curr = q.front();
    int h = curr.first;
    int w = curr.second;
    q.pop_front();
    if (h > 0 && dist[h - 1][w] == -1) {
      q.push_back(make_pair(h - 1, w));
      dist[h - 1][w] = dist[h][w] + 1;
    }
    if (h < H - 1 && dist[h + 1][w] == -1) {
      q.push_back(make_pair(h + 1, w));
      dist[h + 1][w] = dist[h][w] + 1;
    }
    if (w > 0 && dist[h][w - 1] == -1) {
      q.push_back(make_pair(h, w - 1));
      dist[h][w - 1] = dist[h][w] + 1;
    }
    if (w < W - 1 && dist[h][w + 1] == -1) {
      q.push_back(make_pair(h, w + 1));
      dist[h][w + 1] = dist[h][w] + 1;
    }
  }

  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      ans = max(dist[i][j], ans);
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;

int main() {
  typedef pair<int, int> pair;
  int H, W;
  cin >> H >> W;
  vector<string> maze(H);
  vector<vector<int>> dist(H, vector<int>(W, -1));
  for (int i = 0; i < H; i++)
    cin >> maze[i];

  deque<pair> q;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (maze[i][j] == '.')
        continue;
      q.push_back(make_pair(i, j));
      dist[i][j] = 0;
    }
  }

  while (!q.empty()) {
    auto curr = q.front();
    int h = curr.first;
    int w = curr.second;
    q.pop_front();
    if (h > 0 && dist[h - 1][w] == -1) {
      q.push_back(make_pair(h - 1, w));
      dist[h - 1][w] = dist[h][w] + 1;
    }
    if (h < H - 1 && dist[h + 1][w] == -1) {
      q.push_back(make_pair(h + 1, w));
      dist[h + 1][w] = dist[h][w] + 1;
    }
    if (w > 0 && dist[h][w - 1] == -1) {
      q.push_back(make_pair(h, w - 1));
      dist[h][w - 1] = dist[h][w] + 1;
    }
    if (w < W - 1 && dist[h][w + 1] == -1) {
      q.push_back(make_pair(h, w + 1));
      dist[h][w + 1] = dist[h][w] + 1;
    }
  }

  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      ans = max(dist[i][j], ans);
    }
  }
  cout << ans << endl;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long H, W;
  cin >> H >> W;
  vector<vector<long>> data(H, vector<long>(W, INT_MAX));
  char a;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> a;
      if (a == '#')
        data.at(i).at(j) = 0;
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 1; j < W; j++) {
      data.at(i).at(j) = min(data.at(i).at(j - 1) + 1, data.at(i).at(j));
    }
    for (int j = W - 2; j >= 0; j--) {
      data.at(i).at(j) = min(data.at(i).at(j + 1) + 1, data.at(i).at(j));
    }
  }
  for (int i = 0; i < W; i++) {
    for (int j = 1; j < W; j++) {
      data.at(j).at(i) = min(data.at(j - 1).at(i) + 1, data.at(j).at(i));
    }
    for (int j = H - 2; j >= 0; j--) {
      data.at(j).at(i) = min(data.at(j + 1).at(i) + 1, data.at(j).at(i));
    }
  }
  long ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      ans = max(ans, data.at(i).at(j));
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long H, W;
  cin >> H >> W;
  vector<vector<long>> data(H, vector<long>(W, INT_MAX));
  char a;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> a;
      if (a == '#')
        data.at(i).at(j) = 0;
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 1; j < W; j++) {
      data.at(i).at(j) = min(data.at(i).at(j - 1) + 1, data.at(i).at(j));
    }
    for (int j = W - 2; j >= 0; j--) {
      data.at(i).at(j) = min(data.at(i).at(j + 1) + 1, data.at(i).at(j));
    }
  }
  for (int i = 0; i < W; i++) {
    for (int j = 1; j < H; j++) {
      data.at(j).at(i) = min(data.at(j - 1).at(i) + 1, data.at(j).at(i));
    }
    for (int j = H - 2; j >= 0; j--) {
      data.at(j).at(i) = min(data.at(j + 1).at(i) + 1, data.at(j).at(i));
    }
  }
  long ans = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      ans = max(ans, data.at(i).at(j));
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
string mp[1111];
int h, w;
int num[1111][1111];
int vx[4] = {1, 0, -1, 0};
int vy[4] = {0, -1, 0, 1};
bool check(int x, int y) {
  return x >= 0 && x < h && y >= 0 && y < w && num[x][y] == (1e9);
}
int main() {
  cin >> h >> w;
  for (int i = 0; i < 1111; i++)
    for (int j = 0; j < 1111; j++)
      num[i][j] = 1e9;
  queue<pair<pair<int, int>, int>> que;
  for (int i = 0; i < h; i++) {
    cin >> mp[i];
    for (int j = 0; j < w; j++) {
      if (mp[i][j] == '#') {
        num[i][j] = 0;
        que.push(make_pair(make_pair(i, j), 0));
      }
    }
  }
  while (!que.empty()) {
    pair<int, int> now = que.front().first;
    int cnt = que.front().second;
    for (int i = 0; i < 4; i++) {
      int x = vx[i] + now.first, y = vy[i] + now.second;
      if (check(x, y)) {
        num[x][y] = cnt + 1;
        que.push(make_pair(make_pair(x, y), cnt + 1));
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      ans = max(ans, num[i][j]);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
string mp[1111];
int h, w;
int num[1111][1111];
int vx[4] = {1, 0, -1, 0};
int vy[4] = {0, -1, 0, 1};
bool check(int x, int y) {
  return x >= 0 && x < h && y >= 0 && y < w && num[x][y] == (1e9);
}
int main() {
  cin >> h >> w;
  for (int i = 0; i < 1111; i++)
    for (int j = 0; j < 1111; j++)
      num[i][j] = 1e9;
  queue<pair<pair<int, int>, int>> que;
  for (int i = 0; i < h; i++) {
    cin >> mp[i];
    for (int j = 0; j < w; j++) {
      if (mp[i][j] == '#') {
        num[i][j] = 0;
        que.push(make_pair(make_pair(i, j), 0));
      }
    }
  }
  while (!que.empty()) {
    pair<int, int> now = que.front().first;
    int cnt = que.front().second;
    que.pop();
    for (int i = 0; i < 4; i++) {
      int x = vx[i] + now.first, y = vy[i] + now.second;
      if (check(x, y)) {
        num[x][y] = cnt + 1;
        que.push(make_pair(make_pair(x, y), cnt + 1));
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      ans = max(ans, num[i][j]);
  cout << ans << endl;
  return 0;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bits/stdc++.h>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdlib.h>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
bool vis[1003][1003];
int ans = 0;
int dist[1003][1003], dir[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
int main() {
  int n, m;
  cin >> n >> m;
  queue<int> qu, qv;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      dist[i][j] = 1e9;
      if (c == '#') {
        vis[i][j] = 1;
        qu.push(i);
        qv.push(j);
        dist[i][j] = 0;
      }
    }
  while (!qu.empty()) {
    int x = qu.front(), y = qv.front();
    qu.pop(), qv.pop();
    for (int i = 0; i < 4; i++) {
      int u = x + dir[i][0], v = y + dir[i][1];
      if (dist[u][v] > dist[x][y] + 1) {
        dist[u][v] = dist[x][y] + 1;
        qu.push(u), qv.push(v);
      }
    }
    ans = max(ans, dist[x][y]);
  }
  cout << ans;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bits/stdc++.h>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdlib.h>
#include <time.h>
#include <utility>
#include <vector>
using namespace std;
bool vis[1003][1003];
int ans = 0;
int dist[1003][1003], dir[4][2] = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
int main() {
  int n, m;
  cin >> n >> m;
  queue<int> qu, qv;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      dist[i][j] = 1e9;
      if (c == '#') {
        vis[i][j] = 1;
        qu.push(i);
        qv.push(j);
        dist[i][j] = 0;
      }
    }
  while (!qu.empty()) {
    int x = qu.front(), y = qv.front();
    qu.pop(), qv.pop();
    for (int i = 0; i < 4; i++) {
      int u = x + dir[i][0], v = y + dir[i][1];
      if (u < 0 || v < 0 || u >= n || v >= m)
        continue;
      if (dist[u][v] > dist[x][y] + 1) {
        dist[u][v] = dist[x][y] + 1;
        qu.push(u), qv.push(v);
      }
    }
    ans = max(ans, dist[x][y]);
  }
  cout << ans;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#ifdef _DEBUG_
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) (void)0
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;

const int MAX = 1000 + 10;
char in[MAX][MAX];
int dis[MAX][MAX];

const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

int main() {
  int H, W;
  scanf("%d%d", &H, &W);
  for (int i = 0; i < H; i++)
    scanf("%s", in[i + 1] + 1);

  memset(dis, -1, sizeof(dis));

  queue<PII> que;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (in[i][j] == '#') {
        dis[i][j] = 0;
        que.push(MP(i, j));
      }
    }
  }

  while (!que.empty()) {
    PII t = que.front();
    que.pop();
    for (int k = 0; k < 4; k++) {
      int nx = t.F + dx[k];
      int ny = t.S + dy[k];
      if (dis[nx][ny] == -1) {
        dis[nx][ny] = dis[t.F][t.S] + 1;
        que.push(MP(nx, ny));
      }
    }
  }

  int ans = 0;
  for (int i = 1; i <= H; i++)
    for (int j = 1; j <= W; j++)
      ans = max(ans, dis[i][j]);
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#ifdef _DEBUG_
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) (void)0
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;

const int MAX = 1000 + 10;
char in[MAX][MAX];
int dis[MAX][MAX];

const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

int main() {
  int H, W;
  scanf("%d%d", &H, &W);
  for (int i = 0; i < H; i++)
    scanf("%s", in[i + 1] + 1);

  memset(dis, -1, sizeof(dis));

  queue<PII> que;
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (in[i][j] == '#') {
        dis[i][j] = 0;
        que.push(MP(i, j));
      }
    }
  }

  while (!que.empty()) {
    PII t = que.front();
    que.pop();
    for (int k = 0; k < 4; k++) {
      int nx = t.F + dx[k];
      int ny = t.S + dy[k];
      if (in[nx][ny] != '.' && in[nx][ny] != '#')
        continue;
      if (dis[nx][ny] == -1) {
        dis[nx][ny] = dis[t.F][t.S] + 1;
        que.push(MP(nx, ny));
      }
    }
  }

  int ans = 0;
  for (int i = 1; i <= H; i++)
    for (int j = 1; j <= W; j++)
      ans = max(ans, dis[i][j]);
  printf("%d\n", ans);
  return 0;
}
Problem p03053 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<int>> d(h + 2, vector<int>(w + 2));
  queue<int> q;
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      if (s[j] == '#')
        q.push((w + 2) * (i + 1) + j + 1);
      else
        d[i + 1][j + 1] = 1e9;
    }
  }
  int res = 0;
  while (q.size()) {
    int p = q.front();
    int r = p / (w + 2);
    int c = p % (w + 2);
    int dr[] = {-1, 0, 1, 0};
    int dc[] = {0, 1, 0, -1};
    int i = 4;
    while (i--) {
      int nr = r + dr[i];
      int nc = c + dc[i];
      if (d[nr][nc] == 1e9) {
        res = d[nr][nc] = d[r][c] + 1;
        q.push((w + 2) * nr + nc);
      }
    }
  }
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<int>> d(h + 2, vector<int>(w + 2));
  queue<int> q;
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      if (s[j] == '#')
        q.push((w + 2) * (i + 1) + j + 1);
      else
        d[i + 1][j + 1] = 1e9;
    }
  }
  int res = 0;
  while (q.size()) {
    int p = q.front();
    q.pop();
    int r = p / (w + 2);
    int c = p % (w + 2);
    int dr[] = {-1, 0, 1, 0};
    int dc[] = {0, 1, 0, -1};
    int i = 4;
    while (i--) {
      int nr = r + dr[i];
      int nc = c + dc[i];
      if (d[nr][nc] == 1e9) {
        res = d[nr][nc] = d[r][c] + 1;
        q.push((w + 2) * nr + nc);
      }
    }
  }
  cout << res << endl;
}
Problem p03053 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>

using namespace std;

int main(void) {
  int h, w;
  cin >> h >> w;
  string s[1000];
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }

  queue<tuple<int, int, int>> q;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#') {
        q.push(make_tuple(i, j, 0));
      }
    }
  }

  int a[1000][1000];
  while (!q.empty()) {
    tuple<int, int, int> t = q.front();
    q.pop();
    a[get<0>(t)][get<1>(t)] = get<2>(t);

    int dx[] = {0, 0, -1, 1};
    int dy[] = {-1, 1, 0, 0};
    for (int i = 0; i <= 4; i++) {
      int nx = get<0>(t) + dx[i];
      int ny = get<1>(t) + dy[i];

      if (nx < 0)
        continue;
      if (nx >= h)
        continue;
      if (ny < 0)
        continue;
      if (ny >= w)
        continue;
      if (s[nx][ny] == '#')
        continue;
      s[nx][ny] = '#';
      q.push(make_tuple(nx, ny, get<2>(t) + 1));
    }
  }

  int mx = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      mx = max(mx, a[i][j]);
    }
  }
  cout << mx << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>

using namespace std;

int main(void) {
  int h, w;
  cin >> h >> w;
  string s[1000];
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }

  queue<tuple<int, int, int>> q;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#') {
        q.push(make_tuple(i, j, 0));
      }
    }
  }

  int a[1000][1000];
  while (!q.empty()) {
    tuple<int, int, int> t = q.front();
    q.pop();
    a[get<0>(t)][get<1>(t)] = get<2>(t);

    int dx[] = {0, 0, -1, 1};
    int dy[] = {-1, 1, 0, 0};
    for (int i = 0; i < 4; i++) {
      int nx = get<0>(t) + dx[i];
      int ny = get<1>(t) + dy[i];

      if (nx < 0)
        continue;
      if (nx >= h)
        continue;
      if (ny < 0)
        continue;
      if (ny >= w)
        continue;
      if (s[nx][ny] == '#')
        continue;
      s[nx][ny] = '#';
      q.push(make_tuple(nx, ny, get<2>(t) + 1));
    }
  }

  int mx = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      mx = max(mx, a[i][j]);
    }
  }
  cout << mx << endl;

  return 0;
}