Problem p03361 (C++)

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

using namespace std;

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

  vector<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] == '#') {
        if (i == 0 && j == 0) {
          if (s[i][j + 1] != '#' && s[i + 1][j] != '#') {
            cout << "No" << endl;
            return 0;
          }
        } else if (i == h - 1 && j == w - 1) {
          if (s[i][j - 1] != '#' && s[i - 1][j] != '#') {
            cout << "No" << endl;
            return 0;
          }
        } else if (i == 0) {
          if (s[i][j + 1] != '#' && s[i][j - 1] != '#' && s[i + 1][j] != '#') {
            cout << "No" << endl;
            return 0;
          }
        } else if (j == 0) {
          if (s[i][j + 1] != '#' && s[i + 1][j] != '#' && s[i - 1][j] != '#') {
            cout << "No" << endl;
            return 0;
          }
        } else if (i == h - 1) {
          if (s[i][j + 1] != '#' && s[i][j - 1] != '#' && s[i - 1][j] != '#') {
            cout << "No" << endl;
            return 0;
          }
        } else if (j == w - 1) {
          if (s[i][j - 1] != '#' && s[i + 1][j] != '#' && s[i - 1][j] != '#') {
            cout << "No" << endl;
            return 0;
          }
        } else {
          if (s[i][j + 1] != '#' && s[i][j - 1] != '#' && s[i + 1][j] != '#' &&
              s[i - 1][j] != '#') {
            cout << "No" << endl;
            return 0;
          }
        }
      }
    }
  }

  cout << "Yes" << endl;
  return 0;
}

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

using namespace std;

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

  vector<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] == '#') {
        if (i == 0 && j == 0) {
          if (s[i][j + 1] != '#' && s[i + 1][j] != '#') {
            cout << "No" << endl;
            return 0;
          }
        } else if (i == h - 1 && j == w - 1) {
          if (s[i][j - 1] != '#' && s[i - 1][j] != '#') {
            cout << "No" << endl;
            return 0;
          }
        } else if (i == 0 && j == w - 1) {
          if (s[i][j - 1] != '#' && s[i + 1][j] != '#') {
            cout << "No" << endl;
            return 0;
          }
        } else if (i == h - 1 && j == 0) {
          if (s[i][j + 1] != '#' && s[i - 1][j] != '#') {
            cout << "No" << endl;
            return 0;
          }
        } else if (i == 0) {
          if (s[i][j + 1] != '#' && s[i][j - 1] != '#' && s[i + 1][j] != '#') {
            cout << "No" << endl;
            return 0;
          }
        } else if (j == 0) {
          if (s[i][j + 1] != '#' && s[i + 1][j] != '#' && s[i - 1][j] != '#') {
            cout << "No" << endl;
            return 0;
          }
        } else if (i == h - 1) {
          if (s[i][j + 1] != '#' && s[i][j - 1] != '#' && s[i - 1][j] != '#') {
            cout << "No" << endl;
            return 0;
          }
        } else if (j == w - 1) {
          if (s[i][j - 1] != '#' && s[i + 1][j] != '#' && s[i - 1][j] != '#') {
            cout << "No" << endl;
            return 0;
          }
        } else {
          if (s[i][j + 1] != '#' && s[i][j - 1] != '#' && s[i + 1][j] != '#' &&
              s[i - 1][j] != '#') {
            cout << "No" << endl;
            return 0;
          }
        }
      }
    }
  }

  cout << "Yes" << endl;
  return 0;
}