Problem p03436 (C++)

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

int main() {
  int H, W, count = 0;
  cin >> H >> W;
  vector<vector<int>> S(H + 2,
                        vector<int>(W + 2)); // 左右に1列ずつ余裕を持たせる
  for (int i = 0; i < H + 2; i++) {
    for (int j = 0; j < W + 2; j++) {
      int tmpb = i * j * (i - (H + 1)) * (j - (W + 1));
      if (tmpb) {
        char tmp;
        cin >> tmp;
        S[i][j] = (tmp == '.' ? 1 : 0); // 白なら1, 黒なら0
        if (S[i][j])
          count++;
      } else {
        S[i][j] = 0;
      }
    }
  }

  vector<vector<int>> isNotChecked(H + 2, vector<int>(W + 2, 1));
  vector<vector<int>> Sd(H + 2, vector<int>(W + 2, -10000)); //-距離
  Sd[1][1] = 0;
  bool trig = true;
  while (trig) {
    priority_queue<tuple<int, int, int>> Q; //-距離、i座標、j座標
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        if (S[i][j] && isNotChecked[i][j])
          Q.push(make_tuple(Sd[i][j], i, j));
      }
    }
    tuple<int, int, int> mintuple = Q.top();
    int mind = get<0>(mintuple), i = get<1>(mintuple), j = get<2>(mintuple);
    isNotChecked[i][j] = 0;
    Sd[i - 1][j] = max(Sd[i - 1][j], Sd[i][j] - 1);
    Sd[i + 1][j] = max(Sd[i + 1][j], Sd[i][j] - 1);
    Sd[i][j - 1] = max(Sd[i][j - 1], Sd[i][j] - 1);
    Sd[i][j + 1] = max(Sd[i][j + 1], Sd[i][j] - 1);
    trig = false;
    for (int m = 1; m <= H; m++) {
      for (int n = 1; n <= W; n++) {
        if (S[n][m] && isNotChecked[n][m])
          trig = true;
      }
    }
  }
  if (-Sd[H][W] >= 10000)
    cout << -1;
  else
    cout << count + Sd[H][W] - 1;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;

int main() {
  int H, W, count = 0;
  cin >> H >> W;
  vector<vector<int>> S(H + 2,
                        vector<int>(W + 2)); // 左右に1列ずつ余裕を持たせる
  for (int i = 0; i < H + 2; i++) {
    for (int j = 0; j < W + 2; j++) {
      int tmpb = i * j * (i - (H + 1)) * (j - (W + 1));
      if (tmpb) {
        char tmp;
        cin >> tmp;
        S[i][j] = (tmp == '.' ? 1 : 0); // 白なら1, 黒なら0
        if (S[i][j])
          count++;
      } else {
        S[i][j] = 0;
      }
    }
  }

  vector<vector<int>> isNotChecked(H + 2, vector<int>(W + 2, 1));
  vector<vector<int>> Sd(H + 2, vector<int>(W + 2, -10000)); //-距離
  Sd[1][1] = 0;
  bool trig = true;
  while (trig) {
    priority_queue<tuple<int, int, int>> Q; //-距離、i座標、j座標
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        if (S[i][j] && isNotChecked[i][j])
          Q.push(make_tuple(Sd[i][j], i, j));
      }
    }
    tuple<int, int, int> mintuple = Q.top();
    int mind = get<0>(mintuple), i = get<1>(mintuple), j = get<2>(mintuple);
    isNotChecked[i][j] = 0;
    Sd[i - 1][j] = max(Sd[i - 1][j], Sd[i][j] - 1);
    Sd[i + 1][j] = max(Sd[i + 1][j], Sd[i][j] - 1);
    Sd[i][j - 1] = max(Sd[i][j - 1], Sd[i][j] - 1);
    Sd[i][j + 1] = max(Sd[i][j + 1], Sd[i][j] - 1);
    trig = false;
    for (int m = 1; m <= H; m++) {
      for (int n = 1; n <= W; n++) {
        if (S[m][n] && isNotChecked[m][n])
          trig = true;
      }
    }
  }
  if (-Sd[H][W] >= 10000)
    cout << -1;
  else
    cout << count + Sd[H][W] - 1;
  return 0;
}