Problem p02735 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long long D = 1e9 + 7;

long long pow2(long long x, long long y) {
  if (y == 0)
    return 1;
  long long ret = pow2(x, y / 2);
  return ret * ret % D * (y % 2 == 1 ? x : 1) % D;
}

char a[105][105];
int n, m;
int dp[105][105];
int s[105][105];

bool b[101][101][101][101];
bool f(int x, int y, int xx, int yy) { return b[x][y][xx][yy]; }
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);

  int i, j;
  cin >> n >> m;
  for (i = 0; i <= n; i++)
    for (j = 0; j <= m; j++)
      dp[i][j] = 1e9;
  for (i = 1; i <= n; i++)
    cin >> a[i] + 1;

  dp[1][0] = dp[0][1] = 0;

  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      s[i][j] = s[i][j - 1] + s[i - 1][j] + (a[i][j] == '#') - s[i - 1][j - 1];

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (a[i][j] == '#')
        b[i][j][i][j] = 1;
      for (int ii = i; ii <= n; ii++) {
        for (int jj = j; jj <= m; jj++) {
          if (ii == i && jj == j)
            continue;
          b[i][j][ii][jj] = ((b[i][j][ii - 1][jj] | b[i][j][ii][jj - 1]) &
                             (a[ii][jj] == '#'));
          /// cout<<i<<" "<<j<<" "<<ii<<" "<<jj<<" "<<b[i][j][ii][jj]<<"\n";
        }
      }
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      dp[i][j] = min({dp[i][j], dp[i - 1][j] + (a[i][j] == '#'),
                      dp[i][j - 1] + (a[i][j] == '#')});
      for (int ii = 0; ii <= i; ii++) {
        for (int jj = 0; jj <= j; jj++) {
          if (f(ii + 1, jj, i, j) || f(ii, jj + 1, i, j))
            dp[i][j] = min(dp[i][j], dp[ii][jj] + 1);
        }
      }
    }
  }
  cout << dp[n][m];
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long D = 1e9 + 7;

long long pow2(long long x, long long y) {
  if (y == 0)
    return 1;
  long long ret = pow2(x, y / 2);
  return ret * ret % D * (y % 2 == 1 ? x : 1) % D;
}

char a[105][105];
int n, m;
int dp[105][105];
int s[105][105];

bool b[102][102][102][102];
bool f(int x, int y, int xx, int yy) {
  if (x == 0 || y == 0 || x > xx || y > yy)
    return 0;
  return b[x][y][xx][yy];
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);

  int i, j;
  cin >> n >> m;
  for (i = 0; i <= n; i++)
    for (j = 0; j <= m; j++)
      dp[i][j] = 1e9;
  for (i = 1; i <= n; i++)
    cin >> a[i] + 1;

  dp[1][0] = dp[0][1] = 0;

  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      s[i][j] = s[i][j - 1] + s[i - 1][j] + (a[i][j] == '#') - s[i - 1][j - 1];

  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (a[i][j] == '#')
        b[i][j][i][j] = 1;
      for (int ii = i; ii <= n; ii++) {
        for (int jj = j; jj <= m; jj++) {
          if (ii == i && jj == j)
            continue;
          b[i][j][ii][jj] = ((b[i][j][ii - 1][jj] | b[i][j][ii][jj - 1]) &
                             (a[ii][jj] == '#'));
          /// cout<<i<<" "<<j<<" "<<ii<<" "<<jj<<" "<<b[i][j][ii][jj]<<"\n";
        }
      }
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      dp[i][j] = min({dp[i][j], dp[i - 1][j] + (a[i][j] == '#'),
                      dp[i][j - 1] + (a[i][j] == '#')});
      for (int ii = 0; ii <= i; ii++) {
        for (int jj = 0; jj <= j; jj++) {
          if (f(ii + 1, jj, i, j) || f(ii, jj + 1, i, j))
            dp[i][j] = min(dp[i][j], dp[ii][jj] + 1);
        }
      }
    }
  }
  cout << dp[n][m];
  return 0;
}