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

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

int main(int argc, char const *argv[]) {
  int H, W;
  string s[50];
  int ans[50][50];
  cin >> H >> W;
  for (int i = 0; i < H; i++)
    cin >> s[i];

  int mcnt = 0;
  for (int h = 0; h < H; h++)
    for (int w = 0; w < W; w++) {
      if (s[h][w] == '#') {
        ans[h][w] = -1;
        continue;
      }

      for (int sh = -1; sh < 2; sh++)
        for (int sw = -1; sw < 2; sw++) {
          if (h + sh < 0 || w + sw < 0)
            continue;
          if (s[h + sh][w + sw] == '#')
            mcnt++;
        }
      ans[h][w] = mcnt;
      mcnt = 0;
    }

  for (int ah = 0; ah < H; ah++) {

    for (int aw = 0; aw < W; aw++) {
      if (ans[ah][aw] == -1) {
        cout << '#';
      } else {
        cout << ans[ah][aw];
      }
    }
    cout << endl;
  }

  return 0;
}


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

int main(int argc, char const *argv[]) {
  int H, W;
  string s[60];
  int ans[60][60];
  cin >> H >> W;
  for (int i = 0; i < H; i++)
    cin >> s[i];

  int mcnt = 0;
  for (int h = 0; h < H; h++)
    for (int w = 0; w < W; w++) {
      if (s[h][w] == '#') {
        ans[h][w] = -1;
        continue;
      }

      for (int sh = -1; sh < 2; sh++)
        for (int sw = -1; sw < 2; sw++) {
          if (h + sh < 0 || w + sw < 0)
            continue;
          if (s[h + sh][w + sw] == '#')
            mcnt++;
        }
      ans[h][w] = mcnt;
      mcnt = 0;
    }

  for (int ah = 0; ah < H; ah++) {

    for (int aw = 0; aw < W; aw++) {
      if (ans[ah][aw] == -1) {
        cout << '#';
      } else {
        cout << ans[ah][aw];
      }
    }
    cout << endl;
  }

  return 0;
}
Problem p03574 (C++)

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

int main() {
  int a, b;
  cin >> a >> b;
  string vec[a];
  for (int i = 0; i < a; i++) {
    cin >> vec[i];
  }
  int ans[a][b];
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      ans[i][j] = 0;
    }
  }
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      if (vec[i][j] == '#') {
        ans[i][j] = 5000;
        if (i > 0) {
          if (j > 0) {
            ans[i - 1][j - 1]++;
          }
          if (j < b - 1) {
            ans[i - 1][j + 1]++;
          }
          ans[i - 1][j]++;
        }
        if (i < b - 1) {
          if (j > 0) {
            ans[i + 1][j - 1]++;
          }
          if (j < b - 1) {
            ans[i + 1][j + 1]++;
          }
          ans[i + 1][j]++;
        }
        if (j > 0) {
          ans[i][j - 1]++;
        }
        if (j < b - 1) {
          ans[i][j + 1]++;
        }
      }
    }
  }
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      if (ans[i][j] >= 5000) {
        cout << '#';
      } else {
        cout << ans[i][j];
      }
    }
    cout << endl;
  }
  return 0;
}

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

int main() {
  int a, b;
  cin >> a >> b;
  string vec[a];
  for (int i = 0; i < a; i++) {
    cin >> vec[i];
  }
  int ans[a][b];
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      ans[i][j] = 0;
    }
  }
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      if (vec[i][j] == '#') {
        ans[i][j] = 5000;
        if (i > 0) {
          if (j > 0) {
            ans[i - 1][j - 1]++;
          }
          if (j < b - 1) {
            ans[i - 1][j + 1]++;
          }
          ans[i - 1][j]++;
        }
        if (i < a - 1) {
          if (j > 0) {
            ans[i + 1][j - 1]++;
          }
          if (j < b - 1) {
            ans[i + 1][j + 1]++;
          }
          ans[i + 1][j]++;
        }
        if (j > 0) {
          ans[i][j - 1]++;
        }
        if (j < b - 1) {
          ans[i][j + 1]++;
        }
      }
    }
  }
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < b; j++) {
      if (ans[i][j] >= 5000) {
        cout << '#';
      } else {
        cout << ans[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, a[50][50] = {{0}};
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < W; j++) {
      if (s[j] == '#') {

        for (int k = -1; k < 2; k++)
          for (int l = -1; l < 2; l++)
            if (i + k >= 0 && j + l >= 0)
              a[i + k][j + l]++;

        a[i][j] = 100;
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++)
      if (a[i][j] > 10)
        cout << "#";
      else
        cout << a[i][j];

    cout << endl;
  }
}


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

int main() {
  int H, W, a[50][50] = {{0}};
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < W; j++) {
      if (s[j] == '#') {

        for (int k = -1; k < 2; k++)
          for (int l = -1; l < 2; l++)
            if (i + k >= 0 && j + l >= 0 && i + k < H && j + l < W)
              a[i + k][j + l]++;

        a[i][j] = 100;
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++)
      if (a[i][j] > 10)
        cout << "#";
      else
        cout << a[i][j];

    cout << endl;
  }
}
Problem p03574 (C++)

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

int main() {
  int n, m;
  cin >> n >> m;
  vector<string> s(m);
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
  }
  // int cnt = 0;
  string p = "0123456789";
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (s[i][j] == '.') {
        int cnt = 0;
        if (i + 1 < n && s[i + 1][j] == '#')
          cnt++;
        if (j + 1 < m && s[i][j + 1] == '#')
          cnt++;
        if (i - 1 >= 0 && s[i - 1][j] == '#')
          cnt++;
        if (j - 1 >= 0 && s[i][j - 1] == '#')
          cnt++;
        if (i + 1 < n && j + 1 < m && s[i + 1][j + 1] == '#')
          cnt++;
        if (i + 1 < n && j - 1 >= 0 && s[i + 1][j - 1] == '#')
          cnt++;
        if (i - 1 >= 0 && j + 1 < m && s[i - 1][j + 1] == '#')
          cnt++;
        if (i - 1 >= 0 && j - 1 >= 0 && s[i - 1][j - 1] == '#')
          cnt++;
        s[i][j] = p[cnt];
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    cout << s[i] << endl;
  }
  return 0;
}


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

int main() {
  int n, m;
  cin >> n >> m;
  vector<string> s(n);
  for (int i = 0; i < n; ++i) {
    cin >> s[i];
  }
  // int cnt = 0;
  string p = "0123456789";
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (s[i][j] == '.') {
        int cnt = 0;
        if (i + 1 < n && s[i + 1][j] == '#')
          cnt++;
        if (j + 1 < m && s[i][j + 1] == '#')
          cnt++;
        if (i - 1 >= 0 && s[i - 1][j] == '#')
          cnt++;
        if (j - 1 >= 0 && s[i][j - 1] == '#')
          cnt++;
        if (i + 1 < n && j + 1 < m && s[i + 1][j + 1] == '#')
          cnt++;
        if (i + 1 < n && j - 1 >= 0 && s[i + 1][j - 1] == '#')
          cnt++;
        if (i - 1 >= 0 && j + 1 < m && s[i - 1][j + 1] == '#')
          cnt++;
        if (i - 1 >= 0 && j - 1 >= 0 && s[i - 1][j - 1] == '#')
          cnt++;
        s[i][j] = p[cnt];
      }
    }
  }
  for (int i = 0; i < n; ++i) {
    cout << s[i] << endl;
  }
  return 0;
}
Problem p03574 (C++)

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

int main() {
  int H, W;
  cin >> H >> W;
  string s[50];
  int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
  int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

  // 入力
  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++) {
      int count = 0;
      if (s[i][j] == '#') {
        cout << '#';
        continue;
      }
      for (int k = 0; k < 8; k++) {
        int ni = i + dy[k];
        int nj = j + dx[k];
        if (ni < 0 or H <= ni)
          continue;
        if (nj < 0 or W <= nj)
          continue;
        if (s[ni][nj] == '#') {
          count++;
        }
      }
      cout << count;
    }
    cout << endl;
  }
}

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

int main() {
  int H, W;
  cin >> H >> W;
  string s[50];
  int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
  int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

  // 入力
  for (int i = 0; i < H; i++) {
    cin >> s[i];
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      int count = 0;
      if (s[i][j] == '#') {
        cout << '#';
        continue;
      }
      for (int k = 0; k < 8; k++) {
        int ni = i + dy[k];
        int nj = j + dx[k];
        if (ni < 0 or H <= ni)
          continue;
        if (nj < 0 or W <= nj)
          continue;
        if (s[ni][nj] == '#') {
          count++;
        }
      }
      cout << count;
    }
    cout << endl;
  }
}
Problem p03574 (C++)

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

#define ll long long

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int h, w;
  cin >> h >> w;
  string s[w];
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#')
        continue;
      int cur = 0;
      for (int di = -1; di <= 1; di++) {
        for (int dj = -1; dj <= 1; dj++) {
          if (!di && !dj)
            continue;
          if (i + di >= 0 && i + di < h && j + dj >= 0 && j + dj < w) {
            if (s[i + di][j + dj] == '#')
              cur++;
          }
        }
        s[i][j] = '0' + (cur);
      }
    }
    cout << s[i] << '\n';
  }
  return 0;
}

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

#define ll long long

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int h, w;
  cin >> h >> w;
  string s[h];
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#')
        continue;
      int cur = 0;
      for (int di = -1; di <= 1; di++) {
        for (int dj = -1; dj <= 1; dj++) {
          if (!di && !dj)
            continue;
          if (i + di >= 0 && i + di < h && j + dj >= 0 && j + dj < w) {
            if (s[i + di][j + dj] == '#')
              cur++;
          }
        }
        s[i][j] = '0' + (cur);
      }
    }
    cout << s[i] << '\n';
  }
  return 0;
}
Problem p03574 (C++)

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

char CFromI(int i) {
  if (i == 0)
    return '0';
  else if (i == 1)
    return '1';
  else if (i == 2)
    return '2';
  else if (i == 3)
    return '3';
  else if (i == 4)
    return '4';
  else if (i == 5)
    return '5';
  else if (i == 6)
    return '6';
  else if (i == 7)
    return '7';
  else if (i == 8)
    return '8';
}

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

  string Lines[H];
  for (int i = 0; i < W; i++)
    cin >> Lines[i];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (Lines[i].at(j) != '#') {
        int count = 0;
        for (int k = -1; k <= 1; k++) {
          if (i + k >= 0 && i + k < H) {
            for (int l = -1; l <= 1; l++) {
              if (j + l >= 0 && j + l < W) {
                if (Lines[i + k].at(j + l) == '#')
                  count++;
              }
            }
          }
        }
        Lines[i].at(j) = CFromI(count);
      }
    }
  }

  for (int i = 0; i < H; i++)
    cout << Lines[i] << endl;

  return 0;
}


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

char CFromI(int i) {
  if (i == 0)
    return '0';
  else if (i == 1)
    return '1';
  else if (i == 2)
    return '2';
  else if (i == 3)
    return '3';
  else if (i == 4)
    return '4';
  else if (i == 5)
    return '5';
  else if (i == 6)
    return '6';
  else if (i == 7)
    return '7';
  else if (i == 8)
    return '8';
}

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

  string Lines[H];
  for (int i = 0; i < H; i++)
    cin >> Lines[i];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (Lines[i].at(j) != '#') {
        int count = 0;
        for (int k = -1; k <= 1; k++) {
          if (i + k >= 0 && i + k < H) {
            for (int l = -1; l <= 1; l++) {
              if (j + l >= 0 && j + l < W) {
                if (Lines[i + k].at(j + l) == '#')
                  count++;
              }
            }
          }
        }
        Lines[i].at(j) = CFromI(count);
      }
    }
  }

  for (int i = 0; i < H; i++)
    cout << Lines[i] << endl;

  return 0;
}
Problem p03574 (C++)

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

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

  string board[h];
  for (int i = 0; i < h; i++)
    cin >> board[i];
  int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
  int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

  for (int i = 0; i < h; i++) {
    for (int j = 0; i < w; j++) {
      if (board[i][j] == '#')
        continue;

      int num = 0;
      for (int d = 0; d < 8; ++d) {
        int ni = i + dy[d];
        int nj = j + dx[d];

        if (ni < 0 || h <= ni)
          continue;
        if (nj < 0 || w <= nj)
          continue;
        if (board[ni][nj] == '#')
          num++;
      }

      board[i][j] = char(num + '0');
    }
  }
  for (int i = 0; i < h; i++) {
    cout << board[i] << endl;
  }
  return 0;
}


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

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

  string board[h];
  for (int i = 0; i < h; i++)
    cin >> board[i];
  int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
  int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (board[i][j] == '#')
        continue;

      int num = 0;
      for (int d = 0; d < 8; ++d) {
        int ni = i + dy[d];
        int nj = j + dx[d];

        if (ni < 0 || h <= ni)
          continue;
        if (nj < 0 || w <= nj)
          continue;
        if (board[ni][nj] == '#')
          num++;
      }

      board[i][j] = char(num + '0');
    }
  }
  for (int i = 0; i < h; i++) {
    cout << board[i] << endl;
  }
  return 0;
}
Problem p03574 (C++)

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

int main() {
  int H, W, sum[60][60] = {0};
  string S[60];
  cin >> H >> W;
  for (int i = 0; i < H; i++)
    cin >> S[i];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '#') {
        for (int k = -1; k <= 1; k++) {
          for (int l = -1; l <= 1; l++)
            sum[i + k][j + l]++;
        }
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '.')
        cout << sum[i][j];
      else
        cout << S[i][j];
    }
    cout << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;

int main() {
  int H, W, sum[60][60] = {0};
  string S[60];
  cin >> H >> W;
  for (int i = 0; i < H; i++)
    cin >> S[i];
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '#') {
        for (int k = -1; k <= 1; k++) {
          for (int l = -1; l <= 1; l++) {
            if (i + k >= 0 && j + l >= 0)
              sum[i + k][j + l]++;
          }
        }
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (S[i][j] == '.')
        cout << sum[i][j];
      else
        cout << S[i][j];
    }
    cout << endl;
  }
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, a, b) for (int i = a; i < b; i++)
using namespace std;

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

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

  vector<string> S(W);
  rep(i, H) { cin >> S[i]; }

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

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

      int cnt = 0;
      rep(d, 8) {
        int ni = i + dx[d];
        int nj = j + dy[d];

        if (ni < 0 || ni >= H)
          continue;
        if (nj < 0 || nj >= W)
          continue;
        if (S[ni][nj] == '#')
          cnt++;
      }

      S[i][j] = char(cnt + '0');
    }
  }

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

Fixed submission:
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, a, b) for (int i = a; i < b; i++)
using namespace std;

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

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

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

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

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

      int cnt = 0;
      rep(d, 8) {
        int ni = i + dx[d];
        int nj = j + dy[d];

        if (ni < 0 || ni >= H)
          continue;
        if (nj < 0 || nj >= W)
          continue;
        if (S[ni][nj] == '#')
          cnt++;
      }

      S[i][j] = char(cnt + '0');
    }
  }

  rep(i, H) { cout << S[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 before = "..................................................", now,
         after;
  cin >> now >> after;
  for (int l = 0; l < H; l++) {
    for (int i = 0; i < W; i++) {
      if (now.at(i) == '#') {
        cout << '#';
      } else {
        int count = 0;
        if (before.at(i) == '#')
          count++;
        if (after.at(i) == '#')
          count++;
        if (i != 0) {
          if (before.at(i - 1) == '#')
            count++;
          if (now.at(i - 1) == '#')
            count++;
          if (after.at(i - 1) == '#')
            count++;
        }
        if (i != W - 1) {
          if (before.at(i + 1) == '#')
            count++;
          if (now.at(i + 1) == '#')
            count++;
          if (after.at(i + 1) == '#')
            count++;
        }
        cout << count;
      }
    }
    cout << endl;
    before = now;
    now = after;
    cin >> after;
    if (l == H - 2)
      after = "..................................................";
  }
}

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

int main() {
  int H, W;
  cin >> H >> W;
  string before = "..................................................", now,
         after = "..................................................";
  cin >> now >> after;
  for (int l = 0; l < H; l++) {
    for (int i = 0; i < W; i++) {
      if (now.at(i) == '#') {
        cout << '#';
      } else {
        int count = 0;
        if (before.at(i) == '#')
          count++;
        if (after.at(i) == '#')
          count++;
        if (i != 0) {
          if (before.at(i - 1) == '#')
            count++;
          if (now.at(i - 1) == '#')
            count++;
          if (after.at(i - 1) == '#')
            count++;
        }
        if (i != W - 1) {
          if (before.at(i + 1) == '#')
            count++;
          if (now.at(i + 1) == '#')
            count++;
          if (after.at(i + 1) == '#')
            count++;
        }
        cout << count;
      }
    }
    cout << endl;
    before = now;
    now = after;
    cin >> after;
    if (l == H - 2)
      after = "..................................................";
  }
}
Problem p03574 (C++)

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

#define int long long
#define INF 1e18

signed main() {
  int x, y;
  cin >> x >> y;
  vector<string> s(x);
  for (int i = 0; i < x; i++)
    cin >> s[i];
  int dx[] = {0, 0, 1, 1, 1, -1, -1, -1};
  int dy[] = {1, -1, 0, 1, -1, 0, 1, -1};

  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      if (s[i][j] == '.') {
        int cnt = 0;
        for (int k = 0; k < 8; k++) {
          int tmpx = i + dx[k];
          int tmpy = j + dy[k];
          if (s[tmpx][tmpy] == '#') {
            cnt++;
          }
        }
        s[i][j] = '0' + cnt;
      }
    }
  }

  for (int i = 0; i < x; i++) {
    cout << s[i] << endl;
  }
}


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

#define int long long
#define INF 1e18

signed main() {
  int x, y;
  cin >> x >> y;
  vector<string> s(x);
  for (int i = 0; i < x; i++)
    cin >> s[i];
  int dx[] = {0, 0, 1, 1, 1, -1, -1, -1};
  int dy[] = {1, -1, 0, 1, -1, 0, 1, -1};

  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      if (s[i][j] == '.') {
        int cnt = 0;
        for (int k = 0; k < 8; k++) {
          int tmpx = i + dx[k];
          int tmpy = j + dy[k];
          if (tmpx < 0 | tmpx >= x | tmpy < 0 | tmpy >= y) {
            continue;
          }
          if (s[tmpx][tmpy] == '#') {
            cnt++;
          }
        }
        s[i][j] = '0' + cnt;
      }
    }
  }

  for (int i = 0; i < x; i++) {
    cout << s[i] << endl;
  }
}
Problem p03574 (C++)

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

int main(void) {
  int H, W;
  cin >> H >> W;
  char s[H][W], m[H][W];
  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] == '.') {
        int count = 0;
        int k = i - 1, l = j - 1;
        for (k <= i + 1; ++k;) {
          for (l <= j + 1; ++l;) {
            if (s[k][l] == '#')
              count += 1;
          }
        }
        cout << count;
      }

      else
        cout << '#';
    }
    cout << endl;
  }
}


Fixed submission:
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

int main(void) {
  int H, W;
  cin >> H >> W;
  char s[H][W], m[H][W];
  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] == '.') {
        int count = 0;
        for (int k = i - 1; k <= i + 1; ++k) {
          for (int l = j - 1; l <= j + 1; ++l) {
            if (s[k][l] == '#' && 0 <= k && k < H && 0 <= l && l < W)
              count += 1;
          }
        }
        cout << count;
      }

      else
        cout << '#';
    }
    cout << endl;
  }
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#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];

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

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (board[i][j] == '#')
        continue;

      int num = 0;
      for (int d = 0; d < 8; d++) {
        const int ni = i + dy[d];
        const int nj = j + dx[d];

        if (nj < 0 || h <= nj)
          continue;
        if (nj < 0 || w <= nj)
          continue;
        if (board[ni][nj] == '#')
          num++;
      }
      board[i][j] = char(num + '0');
    }
  }

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


Fixed submission:
#include <iostream>
#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];

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

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (board[i][j] == '#')
        continue;

      int num = 0;
      for (int d = 0; d < 8; d++) {
        const int ni = i + dy[d];
        const int nj = j + dx[d];

        if (ni < 0 || h <= ni)
          continue;
        if (nj < 0 || w <= nj)
          continue;
        if (board[ni][nj] == '#')
          num++;
      }
      board[i][j] = char(num + '0');
    }
  }

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

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

int main() {
  int H, W;
  cin >> H >> W;
  string s[50];
  int sum;
  int di[8] = {1, 1, 0, -1, -1, -1, 0, 1};
  int dj[8] = {0, 1, 1, 1, 0, -1, -1, -1};

  for (int i = 0; i < H; i++)
    cin >> s[i];

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (s[i].at(j) == '.') {
        sum = 0;
        for (int k = 0; k < 8; k++) {
          int ni = i + di[k];
          int nj = j = dj[k];
          if (ni < H && ni >= 0 && nj < W && nj >= 0)
            if (s[ni].at(nj) == '#')
              sum += 1;
        }
        cout << sum;
      } else {
        cout << "#";
      }
    }
    cout << endl;
  }
}

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

int main() {
  int H, W;
  cin >> H >> W;
  string s[50];
  int sum;
  int di[8] = {1, 1, 0, -1, -1, -1, 0, 1};
  int dj[8] = {0, 1, 1, 1, 0, -1, -1, -1};

  for (int i = 0; i < H; i++)
    cin >> s[i];

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (s[i].at(j) == '.') {
        sum = 0;
        for (int k = 0; k < 8; k++) {
          if (i + di[k] < H && i + di[k] >= 0 && j + dj[k] < W &&
              j + dj[k] >= 0)
            if (s[i + di[k]].at(j + dj[k]) == '#')
              sum += 1;
        }
        cout << sum;
      } else {
        cout << "#";
      }
    }
    cout << endl;
  }
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

// エイリアス
using ll = long long;
using P = pair<int, int>;

int main() {
  // 入力
  int H, W;
  cin >> H >> W;
  vector<string> mp(H);
  for (int i = 0; i < H; i++)
    cin >> mp[i];

  int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
  int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (mp[i][j] == '#') {
        cout << '#';
        continue;
      }
      int cnt = 0;
      for (int k = 0; k < 8; k++) {
        int x = j + dx[k];
        int y = i + dy[k];
        if (mp[y][x] == '#')
          cnt++;
      }
      cout << cnt;
    }
    cout << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

// エイリアス
using ll = long long;
using P = pair<int, int>;

int main() {
  // 入力
  int H, W;
  cin >> H >> W;
  vector<string> mp(H);
  for (int i = 0; i < H; i++)
    cin >> mp[i];

  int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
  int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (mp[i][j] == '#') {
        cout << '#';
        continue;
      }
      int cnt = 0;
      for (int k = 0; k < 8; k++) {
        int x = j + dx[k];
        int y = i + dy[k];
        if (x < 0 || W <= x || y < 0 || H <= y)
          continue;
        if (mp[y][x] == '#')
          cnt++;
      }
      cout << cnt;
    }
    cout << endl;
  }

  return 0;
}
Problem p03574 (C++)

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

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int main() {
  int H, W;
  cin >> H >> W;
  string S[H];
  REP(i, H) { cin >> S[i]; }
  int HW[H][W];
  REP(i, H) {
    REP(j, W) { HW[i][j] = 0; }
  }
  REP(i, H) {
    REP(j, W) {
      if (S[i][j] == '#') {
        FOR(k, -1, 2) {
          FOR(l, -1, 2) { HW[i + k][j + l] += 1; }
        }
      }
    }
  }
  REP(i, H) {
    REP(j, W) {
      if (S[i][j] == '#') {
        cout << '#';
      } else {
        cout << HW[i][j];
      }
      if (j == W - 1) {
        cout << "\n";
      }
    }
  }
}


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

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int main() {
  int H, W;
  cin >> H >> W;
  string S[H];
  REP(i, H) { cin >> S[i]; }
  int HW[H][W];
  REP(i, H) {
    REP(j, W) { HW[i][j] = 0; }
  }
  REP(i, H) {
    REP(j, W) {
      if (S[i][j] == '#') {
        FOR(k, -1, 2) {
          FOR(l, -1, 2) {
            if (i + k < H && i + k > -1 && j + l < W && j + l > -1) {
              HW[i + k][j + l] += 1;
            }
          }
        }
      }
    }
  }
  REP(i, H) {
    REP(j, W) {
      if (S[i][j] == '#') {
        cout << '#';
      } else {
        cout << HW[i][j];
      }
      if (j == W - 1) {
        cout << "\n";
      }
    }
  }
}
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() {
  string map[50];
  int height, width;
  cin >> height >> width;

  rep(i, height) cin >> map[i];

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

  rep(i, height) {
    rep(j, width) {
      if (map[i][j] == '#')
        continue;

      int num = 0;
      rep(k, 8) {
        int ni = i + dy[k];
        int nj = j + dx[k];

        if (ni < 0 || ni > height)
          continue;
        if (nj < 0 || nj > width)
          continue;
        if (map[ni][nj] == '#')
          num++;
      }
      map[i][j] = '0' + num;
    }
  }
  rep(i, height) cout << map[i] << endl;
}

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

int main() {
  string map[51];
  int height, width;
  cin >> height >> width;

  rep(i, height) cin >> map[i];

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

  rep(i, height) {
    rep(j, width) {
      if (map[i][j] == '#')
        continue;

      int num = 0;
      rep(k, 8) {
        int ni = i + dy[k];
        int nj = j + dx[k];

        if (ni < 0 || ni > height)
          continue;
        if (nj < 0 || nj > width)
          continue;
        if (map[ni][nj] == '#')
          num++;
      }
      map[i][j] = '0' + num;
    }
  }
  rep(i, height) cout << map[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;
  vector<vector<char>> vec(H, vector<char>(W));
  vector<vector<char>> mine(H, vector<char>(W, '0'));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> vec.at(i).at(j);
      if (vec.at(i).at(j) == '#') {
        mine.at(i).at(j) = '#';
        for (int k : {-1, 0, 1}) {
          for (int l : {-1, 0, 1}) {
            if (-1 < i + k < H and -1 < j + l < W) {
              if (vec.at(i + k).at(j + l) != '#') {
                int num;
                num = (int)mine.at(i + k).at(j + l);
                num += 1;
                mine.at(i + k).at(j + l) = (char)num;
              }
            }
          }
        }
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << mine.at(i).at(j);
      if (j == W - 1) {
        cout << endl;
      }
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> vec(H, vector<char>(W));
  vector<vector<char>> mine(H, vector<char>(W, '0'));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> vec.at(i).at(j);
      if (vec.at(i).at(j) == '#') {
        mine.at(i).at(j) = '#';
        for (int k : {-1, 0, 1}) {
          for (int l : {-1, 0, 1}) {
            if (-1 < i + k and i + k < H and -1 < j + l and j + l < W) {
              if (vec.at(i + k).at(j + l) != '#') {
                int num;
                num = (int)mine.at(i + k).at(j + l);
                num += 1;
                mine.at(i + k).at(j + l) = (char)num;
              }
            }
          }
        }
      }
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << mine.at(i).at(j);
      if (j == W - 1) {
        cout << endl;
      }
    }
  }
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define INF 100000000000
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define LOOP(i, N) for (int i = 0; i < N; i++)
typedef pair<int, int> P;
// #define int long long
/* define/include */

int main() {
  int H, W;
  string S[50];
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }
  int c = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      c = 0;
      if (S[i][j] == '.') {
        FOR(k, i - 1, i + 2) {
          FOR(l, j - 1, j + 2) {
            if (k < 0 || l < 0)
              continue;
            if (S[k][l] == '#') {
              c++;
            }
          }
        }
        S[i][j] = c + 48;
      }
    }
  }
  for (int i = 0; i < H; i++) {
    cout << S[i] << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define INF 100000000000
using namespace std;
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define LOOP(i, N) for (int i = 0; i < N; i++)
typedef pair<int, int> P;
// #define int long long
/* define/include */

int main() {
  int H, W;
  string S[51];
  cin >> H >> W;
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }
  int c = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      c = 0;
      if (S[i][j] == '.') {
        FOR(k, i - 1, i + 2) {
          FOR(l, j - 1, j + 2) {
            if (k < 0 || l < 0)
              continue;
            if (S[k][l] == '#') {
              c++;
            }
          }
        }
        S[i][j] = c + 48;
      }
    }
  }
  for (int i = 0; i < H; i++) {
    cout << S[i] << endl;
  }
}
Problem p03574 (C++)

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

int main() {
  int a, b, count;
  std::cin >> a >> b;
  std::string str[51];
  // std::vector <std::string> str(a+3,".");
  for (int i = 1; i <= a; i++) {
    std::cin >> str[i];
  }
  for (int i = 0; i <= b + 1; i++) {
    str[0].insert(0, ".");
    str[a + 1].insert(0, ".");
  }
  for (int i = 1; i <= a; i++) {
    str[i].insert(b, ".");
    str[i].insert(0, ".");
  }
  /* for(int i=0;i<=a+1;i++){
     for(int j=0;j<=b+1;j++){
       std::cout<<str[i][j];
     }
     std::cout<<std::endl;
   }*/
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= b; j++) {
      count = 0;
      // std::cout<<str[i][j]<<std::endl;
      if (str[i][j] == '#') {
        std::cout << str[i][j];
        continue;
      } else {
        if (str[i - 1][j - 1] == '#') {
          count++;
        }
        if (str[i - 1][j] == '#') {
          count++;
        }
        if (str[i - 1][j + 1] == '#') {
          count++;
        }
        if (str[i][j - 1] == '#') {
          count++;
        }
        if (str[i][j + 1] == '#') {
          count++;
        }
        if (str[i + 1][j - 1] == '#') {
          count++;
        }
        if (str[i + 1][j] == '#') {
          count++;
        }
        if (str[i + 1][j + 1] == '#') {
          count++;
        }
      }
      std::cout << count;
    }
    std::cout << std::endl;
  }
  /* for(int i=1;i<=a;i++){
     for(int j=1;j<=b;j++){
       std::cout<<str[i][j];
     }
     std::cout<<std::endl;
   }*/
  return 0;
}

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

int main() {
  int a, b, count;
  std::cin >> a >> b;
  std::string str[60];
  // std::vector <std::string> str(a+3,".");
  for (int i = 1; i <= a; i++) {
    std::cin >> str[i];
  }
  for (int i = 0; i <= b + 1; i++) {
    str[0].insert(0, ".");
    str[a + 1].insert(0, ".");
  }
  for (int i = 1; i <= a; i++) {
    str[i].insert(b, ".");
    str[i].insert(0, ".");
  }
  /* for(int i=0;i<=a+1;i++){
     for(int j=0;j<=b+1;j++){
       std::cout<<str[i][j];
     }
     std::cout<<std::endl;
   }*/
  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= b; j++) {
      count = 0;
      // std::cout<<str[i][j]<<std::endl;
      if (str[i][j] == '#') {
        std::cout << str[i][j];
        continue;
      } else {
        if (str[i - 1][j - 1] == '#') {
          count++;
        }
        if (str[i - 1][j] == '#') {
          count++;
        }
        if (str[i - 1][j + 1] == '#') {
          count++;
        }
        if (str[i][j - 1] == '#') {
          count++;
        }
        if (str[i][j + 1] == '#') {
          count++;
        }
        if (str[i + 1][j - 1] == '#') {
          count++;
        }
        if (str[i + 1][j] == '#') {
          count++;
        }
        if (str[i + 1][j + 1] == '#') {
          count++;
        }
      }
      std::cout << count;
    }
    std::cout << std::endl;
  }
  /* for(int i=1;i<=a;i++){
     for(int j=1;j<=b;j++){
       std::cout<<str[i][j];
     }
     std::cout<<std::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;
  cin >> H >> W;

  vector<string> a(H);

  for (int i = 0; i < H; i++) {
    cin >> a.at(i);
  }

  for (int j = 0; j < H; j++) {
    for (int i = 0; i < W; i++) {
      if (a[j].at(i) == '#') {
        cout << '#';
      } else {
        for (int l = -1; l < 2; l++) {
          for (int k = -1; k < 2; k++) {
            if (i + k >= 0 && i + k < H && j + l >= 0 && j + l < W) {
              if (a[j + l].at(i + k) == '#') {
                count++;
              }
            }
          }
        }
        cout << count;
        count = 0;
      }
    }
    cout << endl;
  }
}

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

int main() {
  int H, W, count = 0;
  cin >> H >> W;

  vector<string> a(H);

  for (int i = 0; i < H; i++) {
    cin >> a.at(i);
  }

  for (int j = 0; j < H; j++) {
    for (int i = 0; i < W; i++) {
      if (a[j].at(i) == '#') {
        cout << '#';
      } else {
        for (int l = -1; l < 2; l++) {
          for (int k = -1; k < 2; k++) {
            if (i + k >= 0 && i + k < W && j + l >= 0 && j + l < H) {
              if (a[j + l].at(i + k) == '#') {
                count++;
              }
            }
          }
        }
        cout << count;
        count = 0;
      }
    }
    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<vector<int>> s(h + 1, vector<int>(w + 1));
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      char t;
      cin >> t;
      if (t == '#') {
        s.at(i).at(j) = 9;
        s.at(i).at(j + 1)++;
        s.at(i).at(j - 1)++;
        s.at(i + 1).at(j)++;
        s.at(i + 1).at(j + 1)++;
        s.at(i + 1).at(j - 1)++;
        s.at(i - 1).at(j)++;
        s.at(i - 1).at(j + 1)++;
        s.at(i - 1).at(j - 1)++;
      }
    }
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (s.at(i).at(j) >= 9) {
        cout << '#';
      } else {
        cout << s.at(i).at(j);
      }
      if (j == w) {
        cout << endl;
      }
    }
  }
}

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

int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<int>> s(h + 2, vector<int>(w + 2));
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      char t;
      cin >> t;
      if (t == '#') {
        s.at(i).at(j) = 9;
        s.at(i).at(j + 1)++;
        s.at(i).at(j - 1)++;
        s.at(i + 1).at(j)++;
        s.at(i + 1).at(j + 1)++;
        s.at(i + 1).at(j - 1)++;
        s.at(i - 1).at(j)++;
        s.at(i - 1).at(j + 1)++;
        s.at(i - 1).at(j - 1)++;
      }
    }
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      if (s.at(i).at(j) >= 9) {
        cout << '#';
      } else {
        cout << s.at(i).at(j);
      }
      if (j == w) {
        cout << endl;
      }
    }
  }
}
Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;

int main() {

  int h, w, cnt = 0;
  string s[50];
  cin >> h >> w;

  for (int i = 1; i <= h; i++) {
    cin >> s[i];
  }

  for (int i = 0; i <= h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '.') {
        if (s[i - 1][j - 1] == '#') {
          cnt++;
        }
        if (s[i - 1][j] == '#') {
          cnt++;
        }
        if (s[i - 1][j + 1] == '#') {
          cnt++;
        }
        if (s[i][j - 1] == '#') {
          cnt++;
        }
        if (s[i][j] == '#') {
          cnt++;
        }
        if (s[i][j + 1] == '#') {
          cnt++;
        }
        if (s[i + 1][j - 1] == '#') {
          cnt++;
        }
        if (s[i + 1][j] == '#') {
          cnt++;
        }
        if (s[i + 1][j + 1] == '#') {
          cnt++;
        }
        cout << cnt;
        cnt = 0;
      } else if (s[i][j] == '#') {
        cout << '#';
      }
    }
    cout << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
using namespace std;

int main() {

  int h, w, cnt = 0;
  string s[51];
  cin >> h >> w;

  for (int i = 1; i <= h; i++) {
    cin >> s[i];
  }

  for (int i = 0; i <= h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '.') {
        if (s[i - 1][j - 1] == '#') {
          cnt++;
        }
        if (s[i - 1][j] == '#') {
          cnt++;
        }
        if (s[i - 1][j + 1] == '#') {
          cnt++;
        }
        if (s[i][j - 1] == '#') {
          cnt++;
        }
        if (s[i][j] == '#') {
          cnt++;
        }
        if (s[i][j + 1] == '#') {
          cnt++;
        }
        if (s[i + 1][j - 1] == '#') {
          cnt++;
        }
        if (s[i + 1][j] == '#') {
          cnt++;
        }
        if (s[i + 1][j + 1] == '#') {
          cnt++;
        }
        cout << cnt;
        cnt = 0;
      } else if (s[i][j] == '#') {
        cout << '#';
      }
    }
    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;
  string s[H];
  for (int i = 0; i < H; i++) {
    cin >> s[i];
  }
  int dx[8] = {1, 0, -1, 0, 1, -1, 1, -1};
  int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (s[i].at(j) == '#') {
        continue;
      }
      int num = 0;
      for (int k = 0; k < 8; k++) {
        int ni = i + dy[k];
        int nj = j + dx[k];
        if (s[ni].at(nj) == '#') {
          num++;
        }
      }
      s[i].at(j) = char(num + '0');
    }
    cout << s[i] << endl;
  }
}

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

int main() {
  int H, W;
  cin >> H >> W;
  string s[H];
  for (int i = 0; i < H; i++) {
    cin >> s[i];
  }
  int dx[8] = {1, 0, -1, 0, 1, -1, 1, -1};
  int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (s[i].at(j) == '#') {
        continue;
      }
      int num = 0;
      for (int k = 0; k < 8; k++) {
        int ni = i + dy[k];
        int nj = j + dx[k];
        if (ni < 0 || H <= ni) {
          continue;
        }
        if (nj < 0 || W <= nj) {
          continue;
        }
        if (s[ni].at(nj) == '#') {
          num++;
        }
      }
      s[i].at(j) = char(num + '0');
    }
    cout << s[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 s[50];
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  int count;
  for (int i = 0; i < h; i++) {   // 縦
    for (int j = 0; j < w; j++) { // 横
      count = 0;
      if (s[i][j] == '#') {
        continue;
      } else {
        for (int k = -1; k < 2; k++) {
          for (int l = -1; l < 2; l++) {
            if (i == 0) {
              if (j == 0) {
                if (k == -1 || l == -1) {
                  continue;
                }
              } else if (j == w - 1) {
                if (k == -1 || l == 1) {
                  continue;
                }
              } else if (k == -1) {
                continue;
              }
            } else if (i == h - 1) {
              if (j == 0) {
                if (k == -1 || l == -1) {
                  continue;
                }
              } else if (j == w - 1) {
                if (k == 1 || l == 1) {
                  continue;
                }
              } else if (k == 1) {
                continue;
              }
            } else if (j == 0) {
              if (l == -1) {
                continue;
              }
            } else if (j == w - 1) {
              if (l == 1) {
                continue;
              }
            }
            if (s[i + k][j + l] == '#') {
              count++;
            }
          }
        }
        s[i][j] = count + '0';
      }
    }
  }

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

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

int main() {
  int h, w;
  cin >> h >> w;
  string s[50];
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }
  int count;
  for (int i = 0; i < h; i++) {   // 縦
    for (int j = 0; j < w; j++) { // 横
      count = 0;
      if (s[i][j] == '#') {
        continue;
      } else {
        for (int k = -1; k < 2; k++) {
          for (int l = -1; l < 2; l++) {
            if (i == 0) {
              if (j == 0) {
                if (k == -1 || l == -1) {
                  continue;
                }
              } else if (j == w - 1) {
                if (k == -1 || l == 1) {
                  continue;
                }
              } else if (k == -1) {
                continue;
              }
            } else if (i == h - 1) {
              if (j == 0) {
                if (k == 1 || l == -1) {
                  continue;
                }
              } else if (j == w - 1) {
                if (k == 1 || l == 1) {
                  continue;
                }
              } else if (k == 1) {
                continue;
              }
            } else if (j == 0) {
              if (l == -1) {
                continue;
              }
            } else if (j == w - 1) {
              if (l == 1) {
                continue;
              }
            }
            if (s[i + k][j + l] == '#') {
              count++;
            }
          }
        }
        s[i][j] = count + '0';
      }
    }
  }

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