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];
  string str0, str1, str2;
  int count = 0;

  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }

  if (H == 1) {
    str1 = S[0];

    for (int j = 0; j < W; j++) {
      if (str1.at(j) == '#')
        continue;
      if (j == 0) {
        if (str1.at(j + 1) == '#')
          count++;
      } else if (j == W - 1) {
        if (str1.at(j - 1) == '#')
          count++;
      } else {
        if (str1.at(j + 1) == '#')
          count++;
        if (str1.at(j - 1) == '#')
          count++;
      }

      if (count == 0)
        S[0].at(j) = '0';
      if (count == 1)
        S[0].at(j) = '1';
      if (count == 2)
        S[0].at(j) = '2';
      count = 0;
    }

    cout << S[0] << endl;
  } else if (W == 1) {
    for (int i = 0; i < H; i++) {
      if (i == 0) {
        str1 = S[i];
        str2 = S[i + 1];
      } else if (i == H - 1) {
        str0 = S[i - 1];
        str1 = S[i];
      } else {
        str0 = S[i - 1];
        str1 = S[i];
        str2 = S[i + 1];
      }

      if (str1.at(0) == '#') {
        cout << S[i] << endl;
        continue;
      }

      if (i == 0) {
        if (str2.at(0) == '#')
          count++;
      } else if (i == H - 1) {
        if (str0.at(0) == '#')
          count++;
      } else {
        if (str0.at(0) == '#')
          count++;
        if (str2.at(0) == '#')
          count++;
      }

      if (count == 0)
        S[i].at(0) = '0';
      if (count == 1)
        S[i].at(0) = '1';
      if (count == 2)
        S[i].at(0) = '2';
      count = 0;
      cout << S[i] << endl;
    }

  } else {

    for (int i = 0; i < H; i++) {
      if (i == 0) {
        str1 = S[i];
        str2 = S[i + 1];
      } else if (i == H - 1) {
        str0 = S[i - 1];
        str1 = S[i];
      } else {
        str0 = S[i - 1];
        str1 = S[i];
        str2 = S[i + 1];
      }
      for (int j = 0; j < W; j++) {
        if (str1.at(j) == '#')
          continue;

        if (i == 0) {
          if (j == 0) {
            if (str1.at(j + 1) == '#')
              count++;
            if (str2.at(j) == '#')
              count++;
            if (str2.at(j + 1) == '#')
              count++;
          } else if (j == W - 1) {
            if (str1.at(j - 1) == '#')
              count++;
            if (str2.at(j) == '#')
              count++;
            if (str2.at(j - 1) == '#')
              count++;
          } else {
            if (str1.at(j + 1) == '#')
              count++;
            if (str2.at(j) == '#')
              count++;
            if (str2.at(j + 1) == '#')
              count++;
            if (str1.at(j - 1) == '#')
              count++;
            if (str2.at(j - 1) == '#')
              count++;
          }
        } else if (i == H - 1) {
          if (j == 0) {
            if (str0.at(j) == '#')
              count++;
            if (str0.at(j + 1) == '#')
              count++;
            if (str1.at(j + 1) == '#')
              count++;
          } else if (j == W - 1) {
            if (str0.at(j) == '#')
              count++;
            if (str0.at(j - 1) == '#')
              count++;
            if (str1.at(j - 1) == '#')
              count++;
          } else {
            if (str0.at(j - 1) == '#')
              count++;
            if (str0.at(j) == '#')
              count++;
            if (str0.at(j + 1) == '#')
              count++;
            if (str1.at(j - 1) == '#')
              count++;
            if (str1.at(j + 1) == '#')
              count++;
          }
        } else {
          if (j == 0) {
            if (str0.at(j) == '#')
              count++;
            if (str0.at(j + 1) == '#')
              count++;
            if (str1.at(j + 1) == '#')
              count++;
            if (str2.at(j) == '#')
              count++;
            if (str2.at(j + 1) == '#')
              count++;
          } else if (j == W - 1) {
            if (str0.at(j - 1) == '#')
              count++;
            if (str0.at(j) == '#')
              count++;
            if (str1.at(j - 1) == '#')
              count++;
            if (str2.at(j - 1) == '#')
              count++;
            if (str2.at(j) == '#')
              count++;
          } else {
            if (str0.at(j - 1) == '#')
              count++;
            if (str0.at(j) == '#')
              count++;
            if (str0.at(j + 1) == '#')
              count++;
            if (str1.at(j - 1) == '#')
              count++;
            if (str1.at(j + 1) == '#')
              count++;
            if (str2.at(j - 1) == '#')
              count++;
            if (str2.at(j) == '#')
              count++;
            if (str2.at(j + 1) == '#')
              count++;
          }
        }

        if (count == 0)
          S[i].at(j) = '0';
        if (count == 1)
          S[i].at(j) = '1';
        if (count == 2)
          S[i].at(j) = '2';
        if (count == 3)
          S[i].at(j) = '3';
        if (count == 4)
          S[i].at(j) = '4';
        if (count == 5)
          S[i].at(j) = '5';
        if (count == 6)
          S[i].at(j) = '6';
        if (count == 7)
          S[i].at(j) = '7';
        if (count == 8)
          S[i].at(j) = '8';
        count = 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];
  string str0, str1, str2;
  int count = 0;

  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }

  if (H == 1 && W == 1) {
    str1 = S[0];
    if (str1.at(0) == '.') {
      cout << 0 << endl;
    } else {
      cout << S[0] << endl;
    }
  } else if (H == 1) {
    str1 = S[0];

    for (int j = 0; j < W; j++) {
      if (str1.at(j) == '#')
        continue;
      if (j == 0) {
        if (str1.at(j + 1) == '#')
          count++;
      } else if (j == W - 1) {
        if (str1.at(j - 1) == '#')
          count++;
      } else {
        if (str1.at(j + 1) == '#')
          count++;
        if (str1.at(j - 1) == '#')
          count++;
      }

      if (count == 0)
        S[0].at(j) = '0';
      if (count == 1)
        S[0].at(j) = '1';
      if (count == 2)
        S[0].at(j) = '2';
      count = 0;
    }

    cout << S[0] << endl;
  } else if (W == 1) {
    for (int i = 0; i < H; i++) {
      if (i == 0) {
        str1 = S[i];
        str2 = S[i + 1];
      } else if (i == H - 1) {
        str0 = S[i - 1];
        str1 = S[i];
      } else {
        str0 = S[i - 1];
        str1 = S[i];
        str2 = S[i + 1];
      }

      if (str1.at(0) == '#') {
        cout << S[i] << endl;
        continue;
      }

      if (i == 0) {
        if (str2.at(0) == '#')
          count++;
      } else if (i == H - 1) {
        if (str0.at(0) == '#')
          count++;
      } else {
        if (str0.at(0) == '#')
          count++;
        if (str2.at(0) == '#')
          count++;
      }

      if (count == 0)
        S[i].at(0) = '0';
      if (count == 1)
        S[i].at(0) = '1';
      if (count == 2)
        S[i].at(0) = '2';
      count = 0;
      cout << S[i] << endl;
    }

  } else {

    for (int i = 0; i < H; i++) {
      if (i == 0) {
        str1 = S[i];
        str2 = S[i + 1];
      } else if (i == H - 1) {
        str0 = S[i - 1];
        str1 = S[i];
      } else {
        str0 = S[i - 1];
        str1 = S[i];
        str2 = S[i + 1];
      }
      for (int j = 0; j < W; j++) {
        if (str1.at(j) == '#')
          continue;

        if (i == 0) {
          if (j == 0) {
            if (str1.at(j + 1) == '#')
              count++;
            if (str2.at(j) == '#')
              count++;
            if (str2.at(j + 1) == '#')
              count++;
          } else if (j == W - 1) {
            if (str1.at(j - 1) == '#')
              count++;
            if (str2.at(j) == '#')
              count++;
            if (str2.at(j - 1) == '#')
              count++;
          } else {
            if (str1.at(j + 1) == '#')
              count++;
            if (str2.at(j) == '#')
              count++;
            if (str2.at(j + 1) == '#')
              count++;
            if (str1.at(j - 1) == '#')
              count++;
            if (str2.at(j - 1) == '#')
              count++;
          }
        } else if (i == H - 1) {
          if (j == 0) {
            if (str0.at(j) == '#')
              count++;
            if (str0.at(j + 1) == '#')
              count++;
            if (str1.at(j + 1) == '#')
              count++;
          } else if (j == W - 1) {
            if (str0.at(j) == '#')
              count++;
            if (str0.at(j - 1) == '#')
              count++;
            if (str1.at(j - 1) == '#')
              count++;
          } else {
            if (str0.at(j - 1) == '#')
              count++;
            if (str0.at(j) == '#')
              count++;
            if (str0.at(j + 1) == '#')
              count++;
            if (str1.at(j - 1) == '#')
              count++;
            if (str1.at(j + 1) == '#')
              count++;
          }
        } else {
          if (j == 0) {
            if (str0.at(j) == '#')
              count++;
            if (str0.at(j + 1) == '#')
              count++;
            if (str1.at(j + 1) == '#')
              count++;
            if (str2.at(j) == '#')
              count++;
            if (str2.at(j + 1) == '#')
              count++;
          } else if (j == W - 1) {
            if (str0.at(j - 1) == '#')
              count++;
            if (str0.at(j) == '#')
              count++;
            if (str1.at(j - 1) == '#')
              count++;
            if (str2.at(j - 1) == '#')
              count++;
            if (str2.at(j) == '#')
              count++;
          } else {
            if (str0.at(j - 1) == '#')
              count++;
            if (str0.at(j) == '#')
              count++;
            if (str0.at(j + 1) == '#')
              count++;
            if (str1.at(j - 1) == '#')
              count++;
            if (str1.at(j + 1) == '#')
              count++;
            if (str2.at(j - 1) == '#')
              count++;
            if (str2.at(j) == '#')
              count++;
            if (str2.at(j + 1) == '#')
              count++;
          }
        }

        if (count == 0)
          S[i].at(j) = '0';
        if (count == 1)
          S[i].at(j) = '1';
        if (count == 2)
          S[i].at(j) = '2';
        if (count == 3)
          S[i].at(j) = '3';
        if (count == 4)
          S[i].at(j) = '4';
        if (count == 5)
          S[i].at(j) = '5';
        if (count == 6)
          S[i].at(j) = '6';
        if (count == 7)
          S[i].at(j) = '7';
        if (count == 8)
          S[i].at(j) = '8';
        count = 0;
      }

      cout << S[i] << endl;
    }
  }
}