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

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

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

  string dummy;
  for (int j = 0; j < W; j++) {
    dummy += '.';
  }

  string S_0, S_1, S_2;
  for (int i = 0; i < H; i++) {
    if (i == 0) {
      S_0 = dummy;
      cin >> S_1;
      cin >> S_2;
      S_0 = '.' + S_0 + '.';
      S_1 = '.' + S_1 + '.';
    } else if (i != H - 1) {
      S_0 = S_1;
      S_1 = S_2;
      cin >> S_2;
    } else {
      S_0 = S_1;
      S_1 = S_2;
      S_2 = dummy;
    }
    S_2 = '.' + S_2 + '.';

    for (int j = 1; j < W + 1; j++) {
      //      cout << S_1 << endl;
      //      cout << " i:" << i << " j:" << j << " ";
      if (S_1.at(j) == '#')
        cout << '#';
      else
        cout << ((S_0.at(j - 1) == '#') + (S_0.at(j) == '#') +
                 (S_0.at(j + 1) == '#') + (S_1.at(j - 1) == '#') +
                 (S_1.at(j) == '#') + (S_1.at(j + 1) == '#') +
                 (S_2.at(j - 1) == '#') + (S_2.at(j) == '#') +
                 (S_2.at(j + 1) == '#'));
    }
    cout << endl;
  }
}


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

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

  string dummy;
  for (int j = 0; j < W; j++) {
    dummy += '.';
  }

  string S_0, S_1, S_2;
  for (int i = 0; i < H; i++) {
    if (i == 0) {
      S_0 = dummy;
      cin >> S_1;
      if (H == 1)
        S_2 = dummy;
      else
        cin >> S_2;
      S_0 = '.' + S_0 + '.';
      S_1 = '.' + S_1 + '.';
    } else if (i != H - 1) {
      S_0 = S_1;
      S_1 = S_2;
      cin >> S_2;
    } else {
      S_0 = S_1;
      S_1 = S_2;
      S_2 = dummy;
    }
    S_2 = '.' + S_2 + '.';

    for (int j = 1; j < W + 1; j++) {
      //      cout << S_1 << endl;
      //      cout << " i:" << i << " j:" << j << " ";
      if (S_1.at(j) == '#')
        cout << '#';
      else
        cout << ((S_0.at(j - 1) == '#') + (S_0.at(j) == '#') +
                 (S_0.at(j + 1) == '#') + (S_1.at(j - 1) == '#') +
                 (S_1.at(j) == '#') + (S_1.at(j + 1) == '#') +
                 (S_2.at(j - 1) == '#') + (S_2.at(j) == '#') +
                 (S_2.at(j + 1) == '#'));
    }
    cout << endl;
  }
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cmath>
#include <functional>
#include <iostream>
#include <set>
#include <vector>

#define r(i, v, n) for (int i = v; i < n; i++)
#define re(i, v, n) for (int i = v; i <= n; i++)

using namespace std;

int main() {
  unsigned long h, w;
  string s;

  cin >> h >> w;
  auto vc = vector<vector<char>>(h, vector<char>(w));
  auto vi = vector<vector<int>>(h * 3, vector<int>(w * 3, 0));

  r(i, 0, h) {
    cin >> s;
    vc[i] = vector<char>(s.begin(), s.end());
  }

  r(i, 0, h) {
    r(j, 0, w) {
      if ('#' == vc[i][j]) {
        vi[h + i - 1][w + j - 1]++;
        vi[h + i - 1][w + j + 2]--;
        vi[h + i + 2][w + j - 1]--;
        vi[h + i + 2][w + j + 2]++;
      }
    }
  }

  r(i, 0, h * 3) {
    r(j, 0, w * 3) {
      if (i == 0 && j == 0) {
        continue;
      } else if (i == 0 && j != 0) {
        vi[i][j] += vi[i][j - 1];
      } else if (i != 0 && j == 0) {
        vi[i][j] += vi[i - 1][j];
      } else {
        vi[i][j] += vi[i][j - 1] + vi[i - 1][j] - vi[i - 1][j - 1];
      }
    }
  }

  r(i, 0, h) {
    r(j, 0, w) {
      if ('#' == vc[i][j]) {
        cout << vc[i][j];
      } else {
        cout << vi[h + i][w + j];
      }
    }
    cout << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <functional>
#include <iostream>
#include <set>
#include <vector>

#define r(i, v, n) for (int i = v; i < n; i++)
#define re(i, v, n) for (int i = v; i <= n; i++)

using namespace std;

int main() {
  unsigned long h, w;
  string s;

  cin >> h >> w;
  auto vc = vector<vector<char>>(h * 2, vector<char>(w * 2));
  auto vi = vector<vector<int>>(h * 4, vector<int>(w * 4, 0));

  r(i, 0, h) {
    cin >> s;
    vc[i] = vector<char>(s.begin(), s.end());
  }

  r(i, 0, h) {
    r(j, 0, w) {
      if ('#' == vc[i][j]) {
        vi[h + i - 1][w + j - 1]++;
        vi[h + i - 1][w + j + 2]--;
        vi[h + i + 2][w + j - 1]--;
        vi[h + i + 2][w + j + 2]++;
      }
    }
  }

  r(i, 0, h * 3) {
    r(j, 0, w * 3) {
      if (i == 0 && j == 0) {
        continue;
      } else if (i == 0 && j != 0) {
        vi[i][j] += vi[i][j - 1];
      } else if (i != 0 && j == 0) {
        vi[i][j] += vi[i - 1][j];
      } else {
        vi[i][j] += vi[i][j - 1] + vi[i - 1][j] - vi[i - 1][j - 1];
      }
    }
  }

  r(i, 0, h) {
    r(j, 0, w) {
      if ('#' == vc[i][j]) {
        cout << vc[i][j];
      } else {
        cout << vi[h + i][w + j];
      }
    }
    cout << endl;
  }

  return 0;
}
Problem p03574 (C++)

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

int main() {
  int H, W, N[50][50] = {0};
  cin >> H >> W;
  char S[50][50], T[50][50];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> S[i][j];
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '#') {
        N[i][j]++;
        N[i][j + 1]++;
        N[i][j + 2]++;
        N[i + 1][j]++;
        N[i + 1][j + 2]++;
        N[i + 2][j]++;
        N[i + 2][j + 1]++;
        N[i + 2][j + 2]++;
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      T[i][j] = '0' + N[i + 1][j + 1];
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '#')
        T[i][j] = S[i][j];
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << T[i][j];
    }
    cout << endl;
  }
}

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

int main() {
  int H, W, N[60][60] = {0};
  cin >> H >> W;
  char S[50][50], T[50][50];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> S[i][j];
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '#') {
        N[i][j]++;
        N[i][j + 1]++;
        N[i][j + 2]++;
        N[i + 1][j]++;
        N[i + 1][j + 2]++;
        N[i + 2][j]++;
        N[i + 2][j + 1]++;
        N[i + 2][j + 2]++;
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      T[i][j] = '0' + N[i + 1][j + 1];
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '#')
        T[i][j] = S[i][j];
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << T[i][j];
    }
    cout << endl;
  }
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int h, w;

int count_bom(vector<string> grid, int i, int j) {
  int count = 0;
  if (i != 0 && j != 0 && grid[i - 1][j - 1] == '#')
    count++;
  if (i != 0 && grid[i - 1][j] == '#')
    count++;
  if (j != w && grid[i - 1][j + 1] == '#')
    count++;
  if (j != 0 && grid[i][j - 1] == '#')
    count++;
  if (j != w && grid[i][j + 1] == '#')
    count++;
  if (i != h && j != 0 && grid[i + 1][j - 1] == '#')
    count++;
  if (i != h && grid[i + 1][j] == '#')
    count++;
  if (i != h && j != w && grid[i + 1][j + 1] == '#')
    count++;
  return count;
}

int main() {
  vector<string> grid;
  cin >> h >> w;

  grid = vector<string>(h);
  for (int i = 0; i < h; i++) {
    cin >> grid[i];
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (grid[i][j] == '.') {
        grid[i][j] = '0' + count_bom(grid, i, j);
      }
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cout << grid[i][j];
    }
    cout << endl;
  }
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int h, w;

int count_bom(vector<string> grid, int i, int j) {
  int count = 0;
  for (int a = -1; a < 2; a++) {
    for (int b = -1; b < 2; b++) {
      if (i + a >= 0 && i + a < h && j + b >= 0 && j + b < w &&
          !(a == 0 && b == 0)) {

        count += grid[i + a][j + b] == '#';
      }
    }
  }
  return count;
}

int main() {
  vector<string> grid;
  cin >> h >> w;

  grid = vector<string>(h);
  for (int i = 0; i < h; i++) {
    cin >> grid[i];
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (grid[i][j] == '.') {
        grid[i][j] = '0' + count_bom(grid, i, j);
      }
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cout << grid[i][j];
    }
    cout << endl;
  }
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  char s[h][w];
  int ans[100][100];
  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      cin >> s[i][j];
      if (s[i][j] == '#') {
        ans[i - 1][j - 1]++;
        ans[i - 1][j]++;
        ans[i - 1][j + 1]++;
        ans[i][j - 1]++;
        ans[i][j + 1]++;
        ans[i + 1][j - 1]++;
        ans[i + 1][j]++;
        ans[i + 1][j + 1]++;
      }
    }
  }
  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      if (s[i][j] == '#') {
        cout << '#';
      } else {
        cout << ans[i][j];
      }
    }
    cout << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  char s[100][100];
  int ans[100][100];
  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      cin >> s[i][j];
      if (s[i][j] == '#') {
        ans[i - 1][j - 1]++;
        ans[i - 1][j]++;
        ans[i - 1][j + 1]++;
        ans[i][j - 1]++;
        ans[i][j + 1]++;
        ans[i + 1][j - 1]++;
        ans[i + 1][j]++;
        ans[i + 1][j + 1]++;
      }
    }
  }
  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      if (s[i][j] == '#') {
        cout << '#';
      } else {
        cout << ans[i][j];
      }
    }
    cout << endl;
  }
  return 0;
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int h, w, occ[50][50] = {};
  cin >> h >> w;
  char s[h][w];
  for (int i = 0; i < h; i++) {
    string str;
    cin >> str;
    for (int j = 0; j < w; j++) {
      s[i][j] = str[j];
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#') {
        for (int k = i - 1; k <= i + 1; k++) {
          for (int l = j - 1; l <= j + 1; l++) {
            if (k >= 0 && l >= 0) {
              occ[k][l]++;
            }
          }
        }
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#') {
        cout << '#';
      } else {
        cout << occ[i][j];
      }
    }
    cout << endl;
  }
  return 0;
}


Fixed submission:
#include <iostream>
using namespace std;
int main() {
  int h, w, occ[50][50] = {};
  cin >> h >> w;
  char s[h][w];
  for (int i = 0; i < h; i++) {
    string str;
    cin >> str;
    for (int j = 0; j < w; j++) {
      s[i][j] = str[j];
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#') {
        for (int k = i - 1; k <= i + 1; k++) {
          for (int l = j - 1; l <= j + 1; l++) {
            if (k >= 0 && k <= h - 1 && l >= 0 && l <= w - 1) {
              occ[k][l]++;
            }
          }
        }
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#') {
        cout << '#';
      } else {
        cout << occ[i][j];
      }
    }
    cout << endl;
  }
  return 0;
}
Problem p03574 (C++)

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

int main() {
  int h, w, count = 0;
  string s;
  cin >> h >> w;
  vector<string> vec(h + 2, "");
  for (int i = 0; i < w + 2; i++) {
    vec.at(0) += ".";
    vec.at(h + 1) += ".";
  }
  for (int i = 1; i < h + 1; i++) {
    cin >> vec.at(i);
    vec.at(i) = "." + vec.at(i) + ".";
  }
  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      if (vec.at(i).at(j) == '#') {
        cout << '#';
      } else {
        count = 0;
        for (int k = -1; k < 2; k++) {
          for (int l = -1; l < 2; l++) {
            if (vec.at(k).at(l) == '#') {
              count += 1;
            }
          }
        }
        cout << count;
      }
    }
    cout << endl;
  }
}


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

int main() {
  int h, w, count = 0;
  string s;
  cin >> h >> w;
  vector<string> vec(h + 2, "");
  for (int i = 0; i < w + 2; i++) {
    vec.at(0) += ".";
    vec.at(h + 1) += ".";
  }
  for (int i = 1; i < h + 1; i++) {
    cin >> vec.at(i);
    vec.at(i) = "." + vec.at(i) + ".";
  }
  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      if (vec.at(i).at(j) == '#') {
        cout << '#';
      } else {
        count = 0;
        for (int k = -1; k < 2; k++) {
          for (int l = -1; l < 2; l++) {
            if (vec.at(k + i).at(l + j) == '#') {
              count += 1;
            }
          }
        }
        cout << count;
      }
    }
    cout << endl;
  }
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  string s, ss, sss, s_last = "";
  for (int i = 0; i < w; i++) {
    s += ".";
  }
  s_last = s;
  cin >> ss >> sss;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int num = 0;
      if (ss.at(j) == '#') {
        printf("#");
      } else {

        if (j != 0) {
          if (s.at(j - 1) == '#')
            num++;
          if (ss.at(j - 1) == '#')
            num++;
          if (sss.at(j - 1) == '#')
            num++;
        }

        if (s.at(j) == '#')
          num++;
        if (sss.at(j) == '#')
          num++;

        if (j != w - 1) {
          if (s.at(j + 1) == '#')
            num++;
          if (ss.at(j + 1) == '#')
            num++;
          if (sss.at(j + 1) == '#')
            num++;
        }

        printf("%d", num);
      }
    }
    puts("");
    s = ss;
    ss = sss;
    if (i == h - 2) {
      sss = s_last;
      continue;
    }
    if (i < h - 1)
      cin >> sss;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
using namespace std;

int main() {
  int h, w;
  cin >> h >> w;
  string s, ss, sss, s_last = "";
  for (int i = 0; i < w; i++) {
    s += ".";
  }
  s_last = s;
  cin >> ss;
  if (h > 1)
    cin >> sss;
  else
    sss = s_last;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int num = 0;
      if (ss.at(j) == '#') {
        printf("#");
      } else {

        if (j != 0) {
          if (s.at(j - 1) == '#')
            num++;
          if (ss.at(j - 1) == '#')
            num++;
          if (sss.at(j - 1) == '#')
            num++;
        }

        if (s.at(j) == '#')
          num++;
        if (sss.at(j) == '#')
          num++;

        if (j != w - 1) {
          if (s.at(j + 1) == '#')
            num++;
          if (ss.at(j + 1) == '#')
            num++;
          if (sss.at(j + 1) == '#')
            num++;
        }

        printf("%d", num);
      }
    }
    puts("");
    s = ss;
    ss = sss;
    if (i == h - 2) {
      sss = s_last;
      continue;
    }
    if (i < h - 1)
      cin >> sss;
  }
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define ll long long

#define INF 999999999

using namespace std;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
// input
int h, w;
string field[50];
int nums[51][51];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> h >> w;

  REP(i, h) { cin >> field[i]; }

  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      if (field[i - 1][j - 1] == '#') {
        nums[j - 1][i - 1]++;
        nums[j - 1][i]++;
        nums[j - 1][i + 1]++;
        nums[j][i - 1]++;
        nums[j][i + 1]++;
        nums[j + 1][i - 1]++;
        nums[j + 1][i]++;
        nums[j + 1][i + 1]++;
      }
    }
  }

  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {

      if (field[i - 1][j - 1] == '#') {
        cout << '#';
      } else {
        cout << nums[j][i];
      }
    }
    cout << "\n";
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define ll long long

#define INF 999999999

using namespace std;

int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
// input
int h, w;
string field[50];
int nums[52][52];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> h >> w;

  REP(i, h) { cin >> field[i]; }

  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      if (field[i - 1][j - 1] == '#') {
        nums[j - 1][i - 1]++;
        nums[j - 1][i]++;
        nums[j - 1][i + 1]++;
        nums[j][i - 1]++;
        nums[j][i + 1]++;
        nums[j + 1][i - 1]++;
        nums[j + 1][i]++;
        nums[j + 1][i + 1]++;
      }
    }
  }

  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {

      if (field[i - 1][j - 1] == '#') {
        cout << '#';
      } else {
        cout << nums[j][i];
      }
    }
    cout << "\n";
  }
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
  char a[52][52];
  int i, j;
  for (i = 0; i < 55; i++) {
    for (j = i; j < 55; j++) {
      a[i][j] = a[j][i] = '.';
    }
  }
  int h, w;
  while (cin >> h >> w) {
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        cin >> a[i][j];
      }
    }
    int sum, m, n;
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        if (a[i][j] != '#') {
          sum = 0;
          for (m = -1; m <= 1; m++) {
            for (n = -1; n <= 1; n++) {
              if (a[i + m][j + n] == '#')
                sum++;
            }
          }

          a[i][j] = '0' + sum;
        }
      }
    }
    cout << endl;
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++)
        cout << a[i][j];
      cout << endl;
    }
  }
}

Fixed submission:
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
  char a[52][52];
  int i, j;
  for (i = 0; i < 52; i++) {
    for (j = i; j < 52; j++) {
      a[i][j] = a[j][i] = '.';
    }
  }
  int h, w;
  while (cin >> h >> w) {
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        cin >> a[i][j];
      }
    }
    int sum, m, n;
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++) {
        if (a[i][j] != '#') {
          sum = 0;
          for (m = -1; m <= 1; m++) {
            for (n = -1; n <= 1; n++) {
              if (a[i + m][j + n] == '#')
                sum++;
            }
          }

          a[i][j] = '0' + sum;
        }
      }
    }
    cout << endl;
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++)
        cout << a[i][j];
      cout << endl;
    }
  }
}
Problem p03574 (C++)

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

int main() {
  vector<int> dx = {-1, 0, 1, -1, 1, -1, 0, 1};
  vector<int> dy = {-1, -1, -1, 0, 0, 1, 1, 1};

  int H, W;
  cin >> H >> W;
  vector<string> s(H);
  for (int i = 0; i < H; i++) {
    cin >> s.at(i);
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (s.at(i).at(j) == '#')
        continue;

      int num = 0;
      for (int d = 0; d < 8; d++) {

        int ni = i + dy.at(i);
        int nj = j + dx.at(i);

        if (ni < 0 or H <= ni)
          continue;
        if (nj < 0 or W <= nj)
          continue;
        if (s.at(ni).at(nj) == '#')
          num++;
      }
      s.at(i).at(j) = char(num + '0');
    }
  }
  for (int i = 0; i < H; i++) {
    cout << s.at(i) << endl;
  }
}


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

int main() {
  vector<int> dx = {-1, 0, 1, -1, 1, -1, 0, 1};
  vector<int> dy = {-1, -1, -1, 0, 0, 1, 1, 1};

  int H, W;
  cin >> H >> W;
  vector<string> s(H);
  for (int i = 0; i < H; i++) {
    cin >> s.at(i);
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (s.at(i).at(j) == '#')
        continue;

      int num = 0;
      for (int d = 0; d < 8; d++) {

        int ni = i + dy.at(d);
        int nj = j + dx.at(d);

        if (ni < 0 or H <= ni)
          continue;
        if (nj < 0 or W <= nj)
          continue;
        if (s.at(ni).at(nj) == '#')
          num++;
      }
      s.at(i).at(j) = char(num + '0');
    }
  }
  for (int i = 0; i < H; i++) {
    cout << s.at(i) << endl;
  }
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

string str[51];
int fie[51][51];

int main(void) {
  int h, w;
  string str;
  scanf("%d%d", &h, &w);
  for (int i = 0; i < h; i++) {
    cin >> str;
    for (int j = 0; j < w; j++) {
      if (str[j] == '#') {
        fie[i + 1][j + 1] = -1000;
        for (int k = -1; k <= 1; k++) {
          for (int l = -1; l <= 1; l++) {
            fie[i + 1 + k][j + 1 + l]++;
          }
        }
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      printf("%c", fie[i + 1][j + 1] >= 0 ? ('0' + fie[i + 1][j + 1]) : '#');
    }
    printf("\n");
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

string str[51];
int fie[61][61];

int main(void) {
  int h, w;
  string str;
  scanf("%d%d", &h, &w);
  for (int i = 0; i < h; i++) {
    cin >> str;
    for (int j = 0; j < w; j++) {
      if (str[j] == '#') {
        fie[i + 1][j + 1] = -1000;
        for (int k = -1; k <= 1; k++) {
          for (int l = -1; l <= 1; l++) {
            fie[i + 1 + k][j + 1 + l]++;
          }
        }
      }
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      printf("%c", fie[i + 1][j + 1] >= 0 ? ('0' + fie[i + 1][j + 1]) : '#');
    }
    printf("\n");
  }
  return 0;
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):

#include <iostream>
using namespace std;

int main() {
  char matrix[52][52];
  int Matrix[51][51];
  int H, W;
  cin >> H >> W;
  for (int i = 0; i < 51; i++) {
    for (int j = 0; j < 51; j++) {
      Matrix[i][j] = 0;
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> matrix[i][j];
      if (matrix[i][j] != '#')
        continue;
      Matrix[i - 1][j]++;
      Matrix[i][j - 1]++;
      Matrix[i + 1][j]++;
      Matrix[i][j + 1]++;
      Matrix[i + 1][j + 1]++;
      Matrix[i - 1][j - 1]++;
      Matrix[i + 1][j - 1]++;
      Matrix[i - 1][j + 1]++;
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (matrix[i][j] == '#') {
        cout << "#";
      } else {
        cout << Matrix[i][j];
      }
    }
    cout << endl;
  }
  cout << endl;
  return 0;
}

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  char matrix[52][52];
  int Matrix[52][52];
  int H, W;
  cin >> H >> W;
  for (int i = 0; i < 51; i++) {
    for (int j = 0; j < 51; j++) {
      Matrix[i][j] = 0;
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      cin >> matrix[i][j];
      if (matrix[i][j] != '#')
        continue;
      Matrix[i - 1][j]++;
      Matrix[i][j - 1]++;
      Matrix[i + 1][j]++;
      Matrix[i][j + 1]++;
      Matrix[i + 1][j + 1]++;
      Matrix[i - 1][j - 1]++;
      Matrix[i + 1][j - 1]++;
      Matrix[i - 1][j + 1]++;
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      if (matrix[i][j] == '#') {
        cout << "#";
      } else {
        cout << Matrix[i][j];
      }
    }
    cout << endl;
  }
  cout << endl;
  return 0;
}
Problem p03574 (C++)

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

using namespace std;

int n, m, cnt;
char a[10005][10005];

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == '.') {
        if (a[i - 1][j - 1] == '#') {
          cnt++;
        }
        if (a[i][j - 1] == '#') {
          cnt++;
        }
        if (a[i + 1][j - 1] == '#') {
          cnt++;
        }
        if (a[i - 1][j] == '#') {
          cnt++;
        }
        if (a[i + 1][j] == '#') {
          cnt++;
        }
        if (a[i - 1][j + 1] == '#') {
          cnt++;
        }
        if (a[i][j + 1] == '#') {
          cnt++;
        }
        if (a[i + 1][j + 1] == '#') {
          cnt++;
        }
        a[i][j] = cnt + '0';
        cnt = 0;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cout << a[i][j];
    }
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int n, m, cnt;
char a[1005][1005];

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i][j] == '.') {
        if (a[i - 1][j - 1] == '#') {
          cnt++;
        }
        if (a[i][j - 1] == '#') {
          cnt++;
        }
        if (a[i + 1][j - 1] == '#') {
          cnt++;
        }
        if (a[i - 1][j] == '#') {
          cnt++;
        }
        if (a[i + 1][j] == '#') {
          cnt++;
        }
        if (a[i - 1][j + 1] == '#') {
          cnt++;
        }
        if (a[i][j + 1] == '#') {
          cnt++;
        }
        if (a[i + 1][j + 1] == '#') {
          cnt++;
        }
        a[i][j] = cnt + '0';
        cnt = 0;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cout << a[i][j];
    }
    cout << endl;
  }
  return 0;
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define EPS 1e-14
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define PI 2 * acos(0.0)
#define ALL(a) (a).begin(), (a).end()
#define DEBUG(x) cout << #x << ": " << x << "\n"
#define DEBUG_ARR(a)                                                           \
  REP(i, size(a)) { cout << #a << "[" << i << "]: " << a[i] << "\n"; }
const int VX[] = {0, 1, 0, -1};
const int VY[] = {1, 0, -1, 0};
const long MOD = 1000000007;

int H, W;
string S[50];

int CountMine(int x, int y) {
  int ret = 0;
  for (int i = max(x - 1, 0); i <= min(H, x + 1); i++) {
    for (int j = max(y - 1, 0); j <= min(W, y + 1); j++) {
      if (i == x && j == y)
        continue;
      if (S[i][j] == '#')
        ret++;
    }
  }

  return ret;
}

int main() {
  //	--- I/O 高速化 ---
  cin.tie(0);
  ios::sync_with_stdio(false);
  // --- ここまで ---

  cin >> H >> W;

  REP(i, H) { cin >> S[i]; }

  REP(i, H) {
    REP(j, W) {
      if (S[i][j] == '#')
        cout << "#";
      else
        cout << CountMine(i, j);
    }
    cout << "\n";
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define EPS 1e-14
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define PI 2 * acos(0.0)
#define ALL(a) (a).begin(), (a).end()
#define DEBUG(x) cout << #x << ": " << x << "\n"
#define DEBUG_ARR(a)                                                           \
  REP(i, size(a)) { cout << #a << "[" << i << "]: " << a[i] << "\n"; }
const int VX[] = {0, 1, 0, -1};
const int VY[] = {1, 0, -1, 0};
const long MOD = 1000000007;

int H, W;
string S[51];

int CountMine(int x, int y) {
  int ret = 0;
  for (int i = max(x - 1, 0); i <= min(H, x + 1); i++) {
    for (int j = max(y - 1, 0); j <= min(W, y + 1); j++) {
      if (i == x && j == y)
        continue;
      if (S[i][j] == '#')
        ret++;
    }
  }

  return ret;
}

int main() {
  //	--- I/O 高速化 ---
  cin.tie(0);
  ios::sync_with_stdio(false);
  // --- ここまで ---

  cin >> H >> W;

  REP(i, H) { cin >> S[i]; }

  REP(i, H) {
    REP(j, W) {
      if (S[i][j] == '#')
        cout << "#";
      else
        cout << CountMine(i, j);
    }
    cout << "\n";
  }

  return 0;
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

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

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

  string s[50];
  rep(i, H) { cin >> s[i]; }

  rep(i, H) {
    rep(j, W) {
      if ('#' == s[i][j]) {
        continue;
      }

      int counter = 0;
      rep(k, 8) {
        int tmp_y = i + dy[k];
        int tmp_x = j + dx[k];

        if (0 > tmp_y || H < tmp_y || 0 > tmp_x || W < tmp_x) {
          continue;
        }

        if ('#' == s[tmp_y][tmp_x]) {
          counter++;
        }
      }

      s[i][j] = (char)(counter + '0');
    }
  }

  rep(i, H) { cout << s[i] << endl; }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

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

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

  string s[50];
  rep(i, H) { cin >> s[i]; }

  rep(i, H) {
    rep(j, W) {
      if ('#' == s[i][j]) {
        continue;
      }

      int counter = 0;
      rep(k, 8) {
        int tmp_y = i + dy[k];
        int tmp_x = j + dx[k];

        if (0 > tmp_y || H <= tmp_y || 0 > tmp_x || W <= tmp_x) {
          continue;
        }

        if ('#' == s[tmp_y][tmp_x]) {
          counter++;
        }
      }

      s[i][j] = (char)(counter + '0');
    }
  }

  rep(i, H) { cout << s[i] << endl; }

  return 0;
}
Problem p03574 (C++)

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

typedef long long ll;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;

int H, W;
string board[50];

char bomb_count(int h, int w) {
  int count = 0;
  FOR(dh, -1, 2)
  FOR(dw, -1, 2) if (0 <= h + dh && h + dh <= H && 0 <= w + dw && w + dw <= W &&
                     board[h + dh][w + dw] == '#') count++;
  return '0' + count;
}

int main() {
  cin >> H >> W;
  REP(i, H) cin >> board[i];

  REP(i, H) REP(j, W) if (board[i][j] == '.') board[i][j] = bomb_count(i, j);

  REP(i, H) cout << board[i] << endl;
  return 0;
}

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

typedef long long ll;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;

int H, W;
string board[50];

char bomb_count(int h, int w) {
  int count = 0;
  FOR(dh, -1, 2)
  FOR(dw, -1, 2) if (0 <= h + dh && h + dh < H && 0 <= w + dw && w + dw < W &&
                     board[h + dh][w + dw] == '#') count++;
  return '0' + count;
}

int main() {
  cin >> H >> W;
  REP(i, H) cin >> board[i];

  REP(i, H) REP(j, W) if (board[i][j] == '.') board[i][j] = bomb_count(i, j);

  REP(i, H) cout << board[i] << endl;
  return 0;
}
Problem p03574 (C++)

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

#define F first
#define S second
#define vsort(v) sort((v).begin(), (v).end())
#define vrev(v) reverse((v).begin(), (v).end())

using namespace std;
using ll = long long;

int main(int argc, const char *argv[]) {
  int h, w;
  cin >> h >> w;
  string s;
  vector<string> v(h);
  for (int i = 0; i < h; i++) {
    cin >> s;
    v[i] = s;
  }
  int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
  int dy[8] = {1, -1, 0, -1, 1, 0, 1, -1};
  int x, y, cnt;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cnt = 0;
      if (v[i][j] != '#') {
        for (int k = 0; k < 8; k++) {
          x = j + dx[k];
          y = i + dy[k];
          if (x >= 0 && x <= w && y >= 0 && y <= h) {
            if (v[y][x] == '#') {
              cnt++;
            }
          }
        }
        v[i][j] = cnt + '0';
      }
    }
  }
  for (int i = 0; i < h; i++) {
    cout << v[i] << endl;
  }
  return 0;
}


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

#define F first
#define S second
#define vsort(v) sort((v).begin(), (v).end())
#define vrev(v) reverse((v).begin(), (v).end())

using namespace std;
using ll = long long;

int main(int argc, const char *argv[]) {
  int h, w;
  cin >> h >> w;
  string s;
  vector<string> v(h);
  for (int i = 0; i < h; i++) {
    cin >> s;
    v[i] = s;
  }
  int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
  int dy[8] = {1, -1, 0, -1, 1, 0, 1, -1};
  int x, y, cnt;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cnt = 0;
      if (v[i][j] != '#') {
        for (int k = 0; k < 8; k++) {
          x = j + dx[k];
          y = i + dy[k];
          if (x >= 0 && x < w && y >= 0 && y < h) {
            if (v[y][x] == '#') {
              cnt++;
            }
          }
        }
        v[i][j] = cnt + '0';
      }
    }
  }
  for (int i = 0; i < h; i++) {
    cout << v[i] << endl;
  }
  return 0;
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <string>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  string board[50];
  for (int i = 0; i < H; i++) {
    cin >> board[i];
  }
  int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
  int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1};
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (board[i][j] == '#') {
        continue;
      }
      int cnt = 0;
      for (int k = 0; k < 8; k++) {
        int x = i + dx[k];
        int y = j + dy[k];
        if (x < 0 || x > W)
          continue;
        if (y < 0 || y > H)
          continue;
        if (board[x][y] == '#')
          cnt++;
      }
      board[i][j] = char(cnt + '0');
    }
  }
  for (int i = 0; i < H; i++) {
    cout << board[i] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#include <string>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  string board[50];
  for (int i = 0; i < H; i++) {
    cin >> board[i];
  }
  int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
  int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1};
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (board[i][j] == '#') {
        continue;
      }
      int cnt = 0;
      for (int k = 0; k < 8; k++) {
        int y = i + dy[k];
        int x = j + dx[k];
        if (x >= 0 && x < W && y >= 0 && y < H) {
          if (board[y][x] == '#')
            cnt++;
        }
      }
      board[i][j] = char(cnt + '0');
    }
  }
  for (int i = 0; i < H; i++) {
    cout << board[i] << endl;
  }
}
Problem p03574 (C++)

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

int main() {
  int h, w;
  cin >> h >> w;
  string masu[20];

  for (int i = 0; i < h; i++) {
    cin >> masu[i];
  }
  int x[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
  int y[8] = {-1, -1, -1, 0, 0, 1, 1, 1};

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (masu[i][j] == '#')
        continue;
      int num = 0;
      for (int k = 0; k < 8; k++) {
        int nx = i - x[k];
        int ny = j - y[k];
        if (nx < 0 || h <= nx)
          continue;
        if (ny < 0 || w <= ny)
          continue;
        if (masu[nx][ny] == '#')
          num++;
      }
      masu[i][j] = char(num + '0');
    }
  }

  for (int i = 0; i < h; i++)
    cout << masu[i] << endl;
}

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

int main() {
  int h, w;
  cin >> h >> w;
  string masu[100];

  for (int i = 0; i < h; i++) {
    cin >> masu[i];
  }
  int x[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
  int y[8] = {-1, -1, -1, 0, 0, 1, 1, 1};

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (masu[i][j] == '#')
        continue;
      int num = 0;
      for (int k = 0; k < 8; k++) {
        int nx = i - x[k];
        int ny = j - y[k];
        if (nx < 0 || h <= nx)
          continue;
        if (ny < 0 || w <= ny)
          continue;
        if (masu[nx][ny] == '#')
          num++;
      }
      masu[i][j] = char(num + '0');
    }
  }

  for (int i = 0; i < h; i++)
    cout << masu[i] << endl;
}
Problem p03574 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;

int main() {

  int H, W;
  cin >> H >> W;

  vector<vector<char>> s(H, vector<char>(W));

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

      cin >> s.at(i).at(j);
    }
  }

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

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

      int count = 0;
      if (s.at(i).at(j) == '.') {

        for (int k = 0; k < 8; i++) {

          if (i + dx[k] >= 0 && i + dx[k] < H && j + dy[k] >= 0 &&
              j + dy[k] < W) {

            if (s.at(i + dx[k]).at(j + dy[k]) == '#') {

              count++;
            }
          }
        }

        s.at(i).at(j) = '0' + count;
      }
    }
  }

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

      cout << s.at(i).at(j);
    }
    cout << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int main() {

  int H, W;
  cin >> H >> W;

  vector<vector<char>> s(H, vector<char>(W));

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

      cin >> s.at(i).at(j);
    }
  }

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

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

      int count = 0;
      if (s.at(i).at(j) == '.') {

        for (int k = 0; k < 8; k++) {

          if (i + dx[k] >= 0 && i + dx[k] < H && j + dy[k] >= 0 &&
              j + dy[k] < W) {

            if (s.at(i + dx[k]).at(j + dy[k]) == '#') {

              count++;
            }
          }
        }

        s.at(i).at(j) = '0' + count;
      }
    }
  }

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

      cout << s.at(i).at(j);
    }
    cout << endl;
  }

  return 0;
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  char **arr = new char *[H];
  for (int i = 0; i < H; i++) {
    arr[i] = new char[W];
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> arr[i][j];
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (arr[i][j] == '.') {
        int num = 0;
        if (arr[i - 1][j - 1] == '#')
          num++;
        if (arr[i - 1][j] == '#')
          num++;
        if (arr[i - 1][j + 1] == '#')
          num++;
        if (arr[i][j - 1] == '#')
          num++;
        if (arr[i][j + 1] == '#')
          num++;
        if (arr[i + 1][j - 1] == '#')
          num++;
        if (arr[i + 1][j] == '#')
          num++;
        if (arr[i + 1][j + 1] == '#')
          num++;
        arr[i][j] = '0' + num;
      }
      cout << arr[i][j];
    }
    cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  char **arr = new char *[H];
  for (int i = 0; i < H; i++) {
    arr[i] = new char[W];
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> arr[i][j];
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (arr[i][j] == '.') {
        int num = 0;
        if (i > 0 && j > 0) {
          if (arr[i - 1][j - 1] == '#')
            num++;
        }
        if (i > 0) {
          if (arr[i - 1][j] == '#')
            num++;
        }
        if (i > 0 && j + 1 < W) {
          if (arr[i - 1][j + 1] == '#')
            num++;
        }
        if (j > 0) {
          if (arr[i][j - 1] == '#')
            num++;
        }
        if (j + 1 < W) {
          if (arr[i][j + 1] == '#')
            num++;
        }
        if (i + 1 < H && j > 0) {
          if (arr[i + 1][j - 1] == '#')
            num++;
        }
        if (i + 1 < H) {
          if (arr[i + 1][j] == '#')
            num++;
        }
        if (i + 1 < H && j + 1 < W) {
          if (arr[i + 1][j + 1] == '#')
            num++;
        }
        arr[i][j] = '0' + num;
      }
      cout << arr[i][j];
    }
    cout << endl;
  }
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  vector<string> map(H);
  for (int i = 0; i < map.size(); i++) {
    cin >> map.at(i);
  }
  vector<int> row = {-1, 0, 1};
  vector<int> column = {-1, 0, 1};
  for (int i = 0; i < map.size(); i++) {
    for (int j = 0; j < map.at(0).size(); j++) {
      if (map.at(i).at(j) == '.') {
        int count = 0;
        for (int x : row) {
          for (int y : column) {
            if (0 <= (i + x) && (i + x) <= H && 0 <= (j + y) && (j + y) <= W &&
                map.at(i + x).at(j + y) == '#')
              count++;
            map.at(i).at(j) = '0' + count;
          }
        }
      }
    }
  }

  for (string x : map) {
    cout << x << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  vector<string> map(H);
  for (int i = 0; i < map.size(); i++) {
    cin >> map.at(i);
  }
  vector<int> row = {-1, 0, 1};
  vector<int> column = {-1, 0, 1};
  for (int i = 0; i < map.size(); i++) {
    for (int j = 0; j < map.at(0).size(); j++) {
      if (map.at(i).at(j) == '.') {
        int count = 0;
        for (int x : row) {
          for (int y : column) {
            if (0 <= (i + x) && (i + x) < H && 0 <= (j + y) && (j + y) < W &&
                map.at(i + x).at(j + y) == '#')
              count++;
            map.at(i).at(j) = '0' + count;
          }
        }
      }
    }
  }

  for (string x : map) {
    cout << x << endl;
  }
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (ll i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1LL << 60;
const double pi = acos(-1.0);

int dh[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dw[8] = {1, -1, 0, 1, -1, 0, 1, -1};

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<char>> v(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> v[i][j];
  vector<vector<int>> ans(h, vector<int>(w, 0));
  rep(i, h) {
    rep(j, w) {
      if (v[i][j] == '#') {
        rep(k, 8) { ans[i + dh[k]][j + dw[k]]++; }
      }
    }
  }
  rep(i, h) {
    rep(j, w) {
      if (v[i][j] == '#')
        cout << '#';
      else
        cout << ans[i][j];
    }
    cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (ll i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1LL << 60;
const double pi = acos(-1.0);

int dh[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dw[8] = {1, -1, 0, 1, -1, 0, 1, -1};

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<char>> v(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> v[i][j];
  vector<vector<int>> ans(h, vector<int>(w, 0));
  rep(i, h) {
    rep(j, w) {
      if (v[i][j] == '#') {
        rep(k, 8) {
          int nh = i + dh[k];
          int nw = j + dw[k];
          if (nh < 0 || h <= nh || nw < 0 || w <= nw)
            continue;
          ans[nh][nw]++;
        }
      }
    }
  }
  rep(i, h) {
    rep(j, w) {
      if (v[i][j] == '#')
        cout << '#';
      else
        cout << ans[i][j];
    }
    cout << endl;
  }
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int main() {
  int h, w;
  cin >> h >> w;

  string s[50];
  rep(i, h) { cin >> s[i]; }
  int count = 0;
  rep(i, h) {
    rep(j, w) {
      if (s[i][j] == '.') {
        count = 0;
        for (int k = i - 1; k <= i + 1; k++) {
          for (int l = j - 1; l <= j + 1; l++) {
            if (k >= 0 && l >= 0) {
              if (s[k][l] == '#') {
                count++;
              }
            }
          }
        }
        cout << count;
      } else {
        cout << s[i][j];
      }
    }
    printf("\n");
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int main() {
  int h, w;
  cin >> h >> w;

  string s[55];
  rep(i, h) { cin >> s[i]; }
  int count = 0;
  rep(i, h) {
    rep(j, w) {
      if (s[i][j] == '.') {
        count = 0;
        for (int k = i - 1; k <= i + 1; k++) {
          for (int l = j - 1; l <= j + 1; l++) {
            if (k >= 0 && l >= 0) {
              if (s[k][l] == '#') {
                count++;
              }
            }
          }
        }
        cout << count;
      } else {
        cout << s[i][j];
      }
    }
    printf("\n");
  }

  return 0;
}