Problem p03361 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
#define ull unsigned long long
#define ll long long
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  string p[h];
  for (int i = 0; i < h; i++) {
    cin >> p[i];
  }
  bool jadge = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (p[j][i] == '#') {
        if (j == 0 && i == 0) {
          if (p[0][1] == '.' && p[1][0] == '.') {
            jadge = 1;
          }
        } else if (j == 0 && i == h - 1) {
          if (p[h - 1][1] == '.' && p[h - 2][0] == '.') {
            jadge = 1;
          }
        } else if (j == w - 1 && i == 0) {
          if (p[0][w - 2] == '.' && p[1][w - 1] == '.') {
            jadge = 1;
          }
        } else if (j == w - 1 && i == h - 1) {
          if (p[h - 1][w - 2] == '.' && p[h - 2][w - 1] == '.') {
            jadge = 1;
          }
        } else if (i == 0 && (j != 0 || j != w - 1)) {
          if (p[j - 1][0] == '.' && p[j + 1][0] == '.' && p[j][1] == '.') {
            jadge = 1;
          }
        } else if (j == 0 && (i != 0 || i != h - 1)) {
          if (p[j][i - 1] == '.' && p[j][i + 1] == '.' && p[j + 1][i] == '.') {
            jadge = 1;
          }
        } else if (j == w - 1 && (i != 0 || i != h - 1)) {
          if (p[j][i - 1] == '.' && p[j][i + 1] == '.' && p[j - 1][i] == '.') {
            jadge = 1;
          }
        } else if (i == h - 1 && (j != 0 || j != w - 1)) {
          if (p[j - 1][i] == '.' && p[j + 1][i] == '.' && p[j][i - 1] == '.') {
            jadge = 1;
          }
        }

        else if (p[j + 1][i] == '.' && p[j - 1][i] == '.' &&
                 p[j][i + 1] == '.' && p[j][i - 1] == '.') {
          jadge = 1;
        }
      } else
        continue;

      if (jadge == 1) {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << "Yes" << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>
#define ull unsigned long long
#define ll long long
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  string p[h];
  for (int i = 0; i < h; i++) {
    cin >> p[i];
  }
  bool jadge = 0;
  for (int i = 0; i < w; i++) {
    for (int j = 0; j < h; j++) {
      if (p[j][i] == '#') {
        if (j == 0 && i == 0) {
          if (p[0][1] == '.' && p[1][0] == '.') {
            jadge = 1;
          }
        } else if (j == 0 && i == h - 1) {
          if (p[h - 1][1] == '.' && p[h - 2][0] == '.') {
            jadge = 1;
          }
        } else if (j == w - 1 && i == 0) {
          if (p[0][w - 2] == '.' && p[1][w - 1] == '.') {
            jadge = 1;
          }
        } else if (j == w - 1 && i == h - 1) {
          if (p[h - 1][w - 2] == '.' && p[h - 2][w - 1] == '.') {
            jadge = 1;
          }
        } else if (i == 0 && (j != 0 || j != w - 1)) {
          if (p[j - 1][0] == '.' && p[j + 1][0] == '.' && p[j][1] == '.') {
            jadge = 1;
          }
        } else if (j == 0 && (i != 0 || i != h - 1)) {
          if (p[j][i - 1] == '.' && p[j][i + 1] == '.' && p[j + 1][i] == '.') {
            jadge = 1;
          }
        } else if (j == w - 1 && (i != 0 || i != h - 1)) {
          if (p[j][i - 1] == '.' && p[j][i + 1] == '.' && p[j - 1][i] == '.') {
            jadge = 1;
          }
        } else if (i == h - 1 && (j != 0 || j != w - 1)) {
          if (p[j - 1][i] == '.' && p[j + 1][i] == '.' && p[j][i - 1] == '.') {
            jadge = 1;
          }
        }

        else if (p[j + 1][i] == '.' && p[j - 1][i] == '.' &&
                 p[j][i + 1] == '.' && p[j][i - 1] == '.') {
          jadge = 1;
        }
      } else
        continue;

      if (jadge == 1) {
        cout << "No" << endl;
        return 0;
      }
    }
  }
  cout << "Yes" << endl;

  return 0;
}