Problem p02735 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 1000000007

#define rep(i, n) for (ll(i) = 0LL; (i) < (ll)(n); (i)++)
#define rep2(i, s, e) for (ll(i) = (ll)(s); (i) < (ll)(e); (i)++)
#define repi(i, n) for (ll(i) = 0LL; (i) <= (ll)(n); (i)++)
#define repi2(i, s, e) for (ll(i) = (ll)(s); (i) <= (ll)(e); (i)++)
#define per(i, n) for (ll(i) = (ll)(n)-1LL; (i) >= 0LL; (i)--)
#define per2(i, s, e) for (ll(i) = (ll)(s)-1LL; (i) >= (ll)(e); (i)--)
#define peri(i, n) for (ll(i) = (ll)(n); (i) >= 0LL; (i)--)
#define peri2(i, s, e) for (ll(i) = (ll)(s); (i) >= (ll)(e); (i)--)
#define iter(i, it) for (auto &(i) : (it))

template <typename T, typename U>
ostream &operator<<(ostream &s, const pair<T, U> m) {
  cout << "(" << m.first << ", " << m.second << ")";
  return s;
}
template <typename T, typename U>
ostream &operator<<(ostream &s, const map<T, U> m) {
  ll c = 0;
  cout << "{ ";
  iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  cout << "{ ";
  rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const list<T> &v) {
  ll c = 0;
  cout << "{ ";
  iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}

const pair<ll, ll> dir[] = {{1, 0}, {0, 1}};

int main(void) {
  ll H, W;
  vector<string> S;
  vector<vector<ll>> dp;
  cin >> H >> W;
  S.resize(H);
  dp.resize(H, vector<ll>(W, numeric_limits<ll>::max() - 1));
  rep(h, H) cin >> S[h];
  if (S[0][0] == '#')
    dp[0][0] = 1LL;
  else
    dp[0][0] = 0LL;
  rep(h, H) rep(w, W) {
    rep(i, 2) {
      ll x = w + dir[i].first;
      ll y = h + dir[i].second;
      if (x >= W || y >= H)
        continue;
      dp[x][y] = min(dp[x][y],
                     dp[h][w] + (S[x][y] == '#' && S[h][w] == '.' ? 1LL : 0LL));
    }
  }
  cout << dp[H - 1][W - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define MOD 1000000007

#define rep(i, n) for (ll(i) = 0LL; (i) < (ll)(n); (i)++)
#define rep2(i, s, e) for (ll(i) = (ll)(s); (i) < (ll)(e); (i)++)
#define repi(i, n) for (ll(i) = 0LL; (i) <= (ll)(n); (i)++)
#define repi2(i, s, e) for (ll(i) = (ll)(s); (i) <= (ll)(e); (i)++)
#define per(i, n) for (ll(i) = (ll)(n)-1LL; (i) >= 0LL; (i)--)
#define per2(i, s, e) for (ll(i) = (ll)(s)-1LL; (i) >= (ll)(e); (i)--)
#define peri(i, n) for (ll(i) = (ll)(n); (i) >= 0LL; (i)--)
#define peri2(i, s, e) for (ll(i) = (ll)(s); (i) >= (ll)(e); (i)--)
#define iter(i, it) for (auto &(i) : (it))

template <typename T, typename U>
ostream &operator<<(ostream &s, const pair<T, U> m) {
  cout << "(" << m.first << ", " << m.second << ")";
  return s;
}
template <typename T, typename U>
ostream &operator<<(ostream &s, const map<T, U> m) {
  ll c = 0;
  cout << "{ ";
  iter(i, m) cout << i << (c++ == m.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  cout << "{ ";
  rep(i, v.size()) cout << v[i] << (i == v.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}
template <typename T> ostream &operator<<(ostream &s, const list<T> &v) {
  ll c = 0;
  cout << "{ ";
  iter(i, v) cout << i << (c++ == v.size() - 1 ? " " : ", ");
  cout << "}";
  return s;
}

const pair<ll, ll> dir[] = {{1, 0}, {0, 1}};

int main(void) {
  ll H, W;
  vector<string> S;
  vector<vector<ll>> dp;
  cin >> H >> W;
  S.resize(H);
  dp.resize(H, vector<ll>(W, numeric_limits<ll>::max() - 1));
  rep(h, H) cin >> S[h];
  if (S[0][0] == '#')
    dp[0][0] = 1LL;
  else
    dp[0][0] = 0LL;
  rep(h, H) rep(w, W) {
    rep(i, 2) {
      ll x = w + dir[i].first;
      ll y = h + dir[i].second;
      if (x >= W || y >= H)
        continue;
      dp[y][x] = min(dp[y][x],
                     dp[h][w] + (S[y][x] == '#' && S[h][w] == '.' ? 1LL : 0LL));
    }
  }
  cout << dp[H - 1][W - 1] << endl;
  return 0;
}