Problem p02735 (C++)

Buggy submission (status: Time Limit Exceeded):
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb(i) push_back(i)
#define pp() pop_back()
#define f first
#define s second
#define mk(l, r) make_pair(l, r)
#define all(vc) vc.begin(), vc.end()
#define bg(vc) vc.begin()
#define ed(vc) vc.end()
#define w(t) while (t--)
const ll MAX = 3e5 + 5;
const ll INF = 2e18;
int h, w;
int ans = INT_MAX;
void solve(string s[], int i, int j, int count) {
  if (i == h - 1 && j == w - 1) {
    ans = min(count, ans);
    return;
  }
  if (i < h - 1) {
    if (s[i + 1][j] == '#')
      solve(s, i + 1, j, count + 1);
    else
      solve(s, i + 1, j, count);
  }
  if (j < w - 1) {
    if (s[i][j + 1] == '#')
      solve(s, i, j + 1, count + 1);
    else
      solve(s, i, j + 1, count);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  t = 1;
  // cin>>t;
  w(t) {

    cin >> h >> w;
    string s[h];
    for (int i = 0; i < h; i++)
      cin >> s[i];
    solve(s, 0, 0, 0);
    if (s[0][0] == '#')
      ans++;
    cout << ans << endl;
  }
}
/*ll msb(ll x)
{
        ll k=(ll)log2(x);
        return (ll)(pow(2,k));
}
#define gc getchar_unlocked
void scanint(int &x)
{
    register int c = gc();
    x = 0;
    int neg = 0;
    for(;((c<48 || c>57) && c != '-');c = gc());
    if(c=='-') {neg=1;c=gc();}
    for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
    if(neg) x=-x;
}*/

Fixed submission:
// #pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb(i) push_back(i)
#define pp() pop_back()
#define f first
#define s second
#define mk(l, r) make_pair(l, r)
#define all(vc) vc.begin(), vc.end()
#define bg(vc) vc.begin()
#define ed(vc) vc.end()
#define w(t) while (t--)
const ll MAX = 3e5 + 5;
const ll INF = 2e18;
int h, w;
int ans = INT_MAX;
void solve(string s[], int i, int j, int count) {
  if (i == h - 1 && j == w - 1) {
    ans = min(count, ans);
    return;
  }
  if (i < h - 1) {
    if (s[i + 1][j] == '#')
      solve(s, i + 1, j, count + 1);
    else
      solve(s, i + 1, j, count);
  }
  if (j < w - 1) {
    if (s[i][j + 1] == '#')
      solve(s, i, j + 1, count + 1);
    else
      solve(s, i, j + 1, count);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  t = 1;
  // cin>>t;
  w(t) {

    cin >> h >> w;
    string s[h];
    for (int i = 0; i < h; i++)
      cin >> s[i];
    int dp[h][w];
    memset(dp, 0, sizeof dp);

    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        int count1 = 0, count2 = 0;
        int acth = 0, actw = 0;
        if (s[i][j] == '#')
          count1 = 1, count2 = 1;

        if (i == 0 && j == 0)
          dp[i][j] = count1;
        else if (i - 1 >= 0 && j - 1 >= 0) {
          if (s[i - 1][j] == '#')
            count1 = 0;
          if (s[i][j - 1] == '#')
            count2 = 0;
          dp[i][j] = min(dp[i - 1][j] + count1, dp[i][j - 1] + count2);
        } else if (i - 1 >= 0) {
          if (s[i - 1][j] == '#')
            count1 = 0;
          dp[i][j] = dp[i - 1][j] + count1;
        } else if (j - 1 >= 0) {
          if (s[i][j - 1] == '#')
            count2 = 0;
          dp[i][j] = dp[i][j - 1] + count2;
        }
      }
    cout << dp[h - 1][w - 1] << endl;
  }
}
/*ll msb(ll x)
{
        ll k=(ll)log2(x);
        return (ll)(pow(2,k));
}
#define gc getchar_unlocked
void scanint(int &x)
{
    register int c = gc();
    x = 0;
    int neg = 0;
    for(;((c<48 || c>57) && c != '-');c = gc());
    if(c=='-') {neg=1;c=gc();}
    for(;c>47 && c<58;c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
    if(neg) x=-x;
}*/