Problem p02735 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pb push_back
#define mp make_pair
#define INF (int)1e9
#define cBits(x) __builtin_popcount(x)
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) a / gcd(a, b) * b
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define FORr(i, a, b) for (int i = a; i >= b; i--)
#define MAX3(a, b, c) max(a, max(b, c))
#define MIN3(a, b, c) min(a, min(b, c))
#define eachTest                                                               \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)                                                                  \
    solve();

using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<ll, ll> PLL;
typedef pair<int, int> PII;

const int MOD = 1000000007;

void solve() { ; }

int main() {

  fastio;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  // eachTest

  int h, w;
  cin >> h >> w;
  string board[h];
  FOR(i, 0, h) { cin >> board[i]; }
  int tbl[h][w];
  // tbl[h][w] holds minimum number of blacks encountered when going from [i][j]
  // to [h-1][w-1]
  if (board[h - 1][w - 1] == '#') {
    tbl[h - 1][w - 1] = 1;
  } else {
    tbl[h - 1][w - 1] = 0;
  }
  FORr(i, h - 2, 0) {
    // [i][w-1]

    tbl[i][w - 1] = tbl[i + 1][w - 1];
    if (board[i][w - 1] != board[i + 1][w - 1] && board[i][w - 1] == '#') {
      tbl[i][w - 1]++;
    }
  }
  FORr(j, w - 2, 0) {
    // [h-1][j]

    tbl[h - 1][j] = tbl[h - 1][j + 1];
    if (board[h - 1][j] != board[h - 1][j + 1] && board[h - 1][j] == '#') {
      tbl[h - 1][j]++;
    }
  }
  FORr(i, h - 2, 0) {
    FORr(j, w - 2, 0) {

      // tbl[i][j] = -1;
      ll temph = -1;
      ll tempv = -1;
      if (board[i][j] == board[i + 1][j]) {
        temph = tbl[i + 1][j];
      } else if (board[i][j] == '#') {
        temph = tbl[i + 1][j] + 1;
      } else {
        temph = tbl[i + 1][j];
      }
      if (board[i][j] == board[i][j + 1]) {
        tempv = tbl[i][j + 1];
      } else if (board[i][j] == '#') {
        tempv = tbl[i][j + 1] + 1;
      } else {
        tempv = tbl[i][j + 1];
      }
      tbl[i][j] = min(temph, tempv);
    }
  }

  // FOR(i,0,h) {
  // 	FOR(j,0,w) {
  // 		cout << tbl[i][j] << " ";
  // 	}
  // 	cout << "\n";
  // }
  cout << tbl[0][0] << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define pb push_back
#define mp make_pair
#define INF (int)1e9
#define cBits(x) __builtin_popcount(x)
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) a / gcd(a, b) * b
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define FORr(i, a, b) for (int i = a; i >= b; i--)
#define MAX3(a, b, c) max(a, max(b, c))
#define MIN3(a, b, c) min(a, min(b, c))
#define eachTest                                                               \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)                                                                  \
    solve();

using namespace std;

typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<ll, ll> PLL;
typedef pair<int, int> PII;

const int MOD = 1000000007;

void solve() { ; }

int main() {

  // eachTest

  int h, w;
  cin >> h >> w;
  string board[h];
  FOR(i, 0, h) { cin >> board[i]; }
  int tbl[h][w];
  // tbl[h][w] holds minimum number of blacks encountered when going from [i][j]
  // to [h-1][w-1]
  if (board[h - 1][w - 1] == '#') {
    tbl[h - 1][w - 1] = 1;
  } else {
    tbl[h - 1][w - 1] = 0;
  }
  FORr(i, h - 2, 0) {
    // [i][w-1]

    tbl[i][w - 1] = tbl[i + 1][w - 1];
    if (board[i][w - 1] != board[i + 1][w - 1] && board[i][w - 1] == '#') {
      tbl[i][w - 1]++;
    }
  }
  FORr(j, w - 2, 0) {
    // [h-1][j]

    tbl[h - 1][j] = tbl[h - 1][j + 1];
    if (board[h - 1][j] != board[h - 1][j + 1] && board[h - 1][j] == '#') {
      tbl[h - 1][j]++;
    }
  }
  FORr(i, h - 2, 0) {
    FORr(j, w - 2, 0) {

      // tbl[i][j] = -1;
      ll temph = -1;
      ll tempv = -1;
      if (board[i][j] == board[i + 1][j]) {
        temph = tbl[i + 1][j];
      } else if (board[i][j] == '#') {
        temph = tbl[i + 1][j] + 1;
      } else {
        temph = tbl[i + 1][j];
      }
      if (board[i][j] == board[i][j + 1]) {
        tempv = tbl[i][j + 1];
      } else if (board[i][j] == '#') {
        tempv = tbl[i][j + 1] + 1;
      } else {
        tempv = tbl[i][j + 1];
      }
      tbl[i][j] = min(temph, tempv);
    }
  }

  // FOR(i,0,h) {
  // 	FOR(j,0,w) {
  // 		cout << tbl[i][j] << " ";
  // 	}
  // 	cout << "\n";
  // }
  cout << tbl[0][0] << "\n";

  return 0;
}