Problem p02735 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
#define MOD 1000000007
#define INF 1000000000
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
#define endl '\n'
#define pl cout << endl;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int dp[105][105];
int main() {

#ifndef ONLINE_JUDGE
  freopen("D:\\Ajay\\Codes\\in.txt", "r", stdin);
  freopen("D:\\Ajay\\Codes\\out.txt", "w", stdout);
#endif
  fast();
  int r, c;
  cin >> r >> c;
  string s[105];
  for (int i = 0; i < r; i++)
    cin >> s[i];
  if (s[0][0] == '#')
    dp[0][0] = 1;
  else
    dp[0][0] = 0;
  for (int i = 1; i < c; i++) {
    if (s[0][i] == '.') {
      dp[0][i] = dp[0][i - 1];
    } else {
      if (s[0][i - 1] == '#')
        dp[0][i] = dp[0][i - 1];
      else
        dp[0][i] = dp[0][i - 1] + 1;
    }
  }
  for (int i = 1; i < r; i++) {
    if (s[i][0] == '.') {
      dp[i][0] = dp[i - 1][0];
    } else {
      if (s[i - 1][0] == '#')
        dp[i][0] = dp[i - 1][0];
      else
        dp[i][0] = dp[i - 1][0] + 1;
    }
  }
  for (int i = 1; i < r; i++) {
    for (int j = 1; j < c; j++) {
      if (s[i][j] == '#') {
        if (s[i - 1][j] == '#') {
          if (s[i][j - 1] == '#') {
            dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]);
          } else {
            dp[i][j] = min(dp[i - 1][j], dp[i][j - 1] + 1);
          }
        } else {
          if (s[i][j - 1] == '#') {
            dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1]);
          } else {
            dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1);
          }
        }
      } else {
        dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  cout << dp[r - 1][c - 1];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
#define MOD 1000000007
#define INF 1000000000
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
#define endl '\n'
#define pl cout << endl;
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int dp[105][105];
int main() {

  fast();
  int r, c;
  cin >> r >> c;
  string s[105];
  for (int i = 0; i < r; i++)
    cin >> s[i];
  if (s[0][0] == '#')
    dp[0][0] = 1;
  else
    dp[0][0] = 0;
  for (int i = 1; i < c; i++) {
    if (s[0][i] == '.') {
      dp[0][i] = dp[0][i - 1];
    } else {
      if (s[0][i - 1] == '#')
        dp[0][i] = dp[0][i - 1];
      else
        dp[0][i] = dp[0][i - 1] + 1;
    }
  }
  for (int i = 1; i < r; i++) {
    if (s[i][0] == '.') {
      dp[i][0] = dp[i - 1][0];
    } else {
      if (s[i - 1][0] == '#')
        dp[i][0] = dp[i - 1][0];
      else
        dp[i][0] = dp[i - 1][0] + 1;
    }
  }
  for (int i = 1; i < r; i++) {
    for (int j = 1; j < c; j++) {
      if (s[i][j] == '#') {
        if (s[i - 1][j] == '#') {
          if (s[i][j - 1] == '#') {
            dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]);
          } else {
            dp[i][j] = min(dp[i - 1][j], dp[i][j - 1] + 1);
          }
        } else {
          if (s[i][j - 1] == '#') {
            dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1]);
          } else {
            dp[i][j] = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1);
          }
        }
      } else {
        dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  cout << dp[r - 1][c - 1];
}