Problem p03361 (C++)

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

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

  if (H == 1 && W == 1) {
    cout << "No" << endl;
  } else if (H == 1) {
    for (int col = 0; col < W; col++) {
      if (col == 0) {
        if (s[0][col + 1] == '.') {
          cout << "No" << endl;
          return 0;
        }
      } else if (s[0][col - 1] == '.' && s[0][col + 1] == '.') {
        cout << "No" << endl;
        return 0;
      }
    }
  } else if (W == 1) {
    for (int row = 0; row < H; row++) {
      if (row == 0) {
        if (s[row + 1][0] == '.') {
          cout << "No" << endl;
          return 0;
        }
      } else if (s[row - 1][0] == '.' && s[row + 1][0] == '.') {
        cout << "No" << endl;
        return 0;
      }
    }
  } else {
    for (int row = 0; row < H; row++) {
      for (int col = 0; col < W; col++) {
        // cout << row << ", " << col << endl;

        if (s[row][col] == '#') {
          if (row == 0 && col == 0) {
            if (s[0][1] == '.' && s[1][0] == '.') {
              cout << "No" << endl;
              return 0;
            }
          } else if (row == 0 && col == W - 1) {

            if (s[0][W - 2] == '.' && s[1][W - 1] == '.') {
              cout << "No" << endl;
              return 0;
            }
          } else if (row == H - 1 && col == 0) {

            if (s[H - 2][0] == '.' && s[H - 1][1] == '.') {
              cout << "No" << endl;
              return 0;
            }
          } else if (row == H - 1 && col == W - 1) {

            if (s[H - 2][W - 1] == '.' && s[H - 1][W - 2] == '.') {
              cout << "No" << endl;
              return 0;
            }
          } else if (row == 0) {
            if (s[row][col - 1] == '.' && s[row][col + 1] == '.' &&
                s[row + 1][col] == '.') {
              cout << "No" << endl;
              return 0;
            }
          } else if (row == H - 1) {
            if (s[row][col - 1] == '.' && s[row][col + 1] == '.' &&
                s[row - 1][col] == '.') {
              cout << "No" << endl;
              return 0;
            }
          } else if (col == 0) {
            if (s[row - 1][col] == '.' && s[row + 1][col] == '.' &&
                s[row][col + 1] == '.') {
              cout << "No" << endl;
              return 0;
            }
          } else if (col == W - 1) {
            if (s[row - 1][col] == '.' && s[row + 1][col] == '.' &&
                s[row][col - 1] == '.') {
              cout << "No" << endl;
              return 0;
            }
          } else {
            if (s[row][col - 1] == '.' && s[row][col + 1] == '.' &&
                s[row - 1][col] == '.' && s[row + 1][col] == '.') {
              cout << "No" << endl;
              return 0;
            }
          }
        }
      }
    }
  }

  cout << "Yes" << endl;

  return 0;
}

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

  if (H == 1 && W == 1) {
    cout << "No" << endl;
  } else if (H == 1) {
    for (int col = 0; col < W; col++) {
      if (col == 0) {
        if (s[0][col + 1] == '.') {
          cout << "No" << endl;
          return 0;
        }
      } else if (s[0][col - 1] == '.' && s[0][col + 1] == '.') {
        cout << "No" << endl;
        return 0;
      }
    }
  } else if (W == 1) {
    for (int row = 0; row < H; row++) {
      if (row == 0) {
        if (s[row + 1][0] == '.') {
          cout << "No" << endl;
          return 0;
        }
      } else if (s[row - 1][0] == '.' && s[row + 1][0] == '.') {
        cout << "No" << endl;
        return 0;
      }
    }
  } else {
    for (int row = 0; row < H; row++) {
      for (int col = 0; col < W; col++) {
        // cout << row << ", " << col << endl;

        if (s[row][col] == '#') {
          if (row == 0 && col == 0) {
            if (s[0][1] == '.' && s[1][0] == '.') {
              cout << "No" << endl;
              return 0;
            }
          } else if (row == 0 && col == W - 1) {

            if (s[0][W - 2] == '.' && s[1][W - 1] == '.') {
              cout << "No" << endl;
              return 0;
            }
          } else if (row == H - 1 && col == 0) {

            if (s[H - 2][0] == '.' && s[H - 1][1] == '.') {
              cout << "No" << endl;
              return 0;
            }
          } else if (row == H - 1 && col == W - 1) {

            if (s[H - 2][W - 1] == '.' && s[H - 1][W - 2] == '.') {
              cout << "No" << endl;
              return 0;
            }
          } else if (row == 0) {
            if (s[row][col - 1] == '.' && s[row][col + 1] == '.' &&
                s[row + 1][col] == '.') {
              cout << "No" << endl;
              return 0;
            }
          } else if (row == H - 1) {
            if (s[row][col - 1] == '.' && s[row][col + 1] == '.' &&
                s[row - 1][col] == '.') {
              cout << "No" << endl;
              return 0;
            }
          } else if (col == 0) {
            if (s[row - 1][col] == '.' && s[row + 1][col] == '.' &&
                s[row][col + 1] == '.') {
              cout << "No" << endl;
              return 0;
            }
          } else if (col == W - 1) {
            if (s[row - 1][col] == '.' && s[row + 1][col] == '.' &&
                s[row][col - 1] == '.') {
              cout << "No" << endl;
              return 0;
            }
          } else {
            if (s[row][col - 1] == '.' && s[row][col + 1] == '.' &&
                s[row - 1][col] == '.' && s[row + 1][col] == '.') {
              cout << "No" << endl;
              return 0;
            }
          }
        }
      }
    }
  }

  cout << "Yes" << endl;

  return 0;
}