Problem p03960 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X)                                                                 \
  int(X);                                                                      \
  scanf("%d", &X)
#define DRII(X, Y)                                                             \
  int X, Y;                                                                    \
  scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z)                                                         \
  int X, Y, Z;                                                                 \
  scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET                                                                  \
  int ___T, case_n = 1;                                                        \
  scanf("%d ", &___T);                                                         \
  while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int, int>
#define VI vector<int>
#define VPII vector<pair<int, int>>
#define PLL pair<long long, long long>
#define VPLL vector<pair<long long, long long>>
#define F first
#define S second
typedef long long LL;
using namespace std;
const int MOD = 1e9 + 7;
const int SIZE = 310;
char s[SIZE][SIZE];
int dp[SIZE][SIZE], v[SIZE][SIZE];
void mii(int &x, int v) {
  if (x > v || x == -1)
    x = v;
}
int solve(vector<char> s1, vector<char> s2) {
  MS1(dp);
  MS0(v);
  int H = SZ(s1);
  dp[0][0] = 0;
  REPP(i, 1, H + 1)
  REPP(j, 1, H + 1) {
    REP(k, min(i, j)) v[i][j] += (s1[i - 1 - k] == s2[j - 1 - k]);
  }
  REP(i, H + 1) REP(j, H + 1) {
    mii(dp[i + 1][j], dp[i][j] + v[i + 1][j]);
    mii(dp[i][j + 1], dp[i][j] + v[i][j + 1]);
  }
  return dp[H][H];
}
int main() {
  DRII(H, W);
  REP(i, H) RS(s[i]);
  int an = 0;
  REPP(i, 1, W) {
    vector<char> s1, s2;
    REP(j, H) s1.PB(s[j][i - 1]);
    REP(j, H) s2.PB(s[j][i]);
    an += solve(s1, s2);
  }
  cout << an << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X)                                                                 \
  int(X);                                                                      \
  scanf("%d", &X)
#define DRII(X, Y)                                                             \
  int X, Y;                                                                    \
  scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z)                                                         \
  int X, Y, Z;                                                                 \
  scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET                                                                  \
  int ___T, case_n = 1;                                                        \
  scanf("%d ", &___T);                                                         \
  while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int, int>
#define VI vector<int>
#define VPII vector<pair<int, int>>
#define PLL pair<long long, long long>
#define VPLL vector<pair<long long, long long>>
#define F first
#define S second
typedef long long LL;
using namespace std;
const int MOD = 1e9 + 7;
const int SIZE = 310;
char s[SIZE][SIZE];
int dp[SIZE][SIZE], v[SIZE][SIZE];
void mii(int &x, int v) {
  if (x > v || x == -1)
    x = v;
}
int solve(vector<char> s1, vector<char> s2) {
  MS1(dp);
  MS0(v);
  int H = SZ(s1);
  dp[0][0] = 0;
  REPP(i, 1, H + 1)
  REPP(j, 1, H + 1) {
    v[i][j] = v[i - 1][j - 1] + (s1[i - 1] == s2[j - 1]);
    // REP(k,min(i,j))v[i][j]+=(s1[i-1-k]==s2[j-1-k]);
  }
  REP(i, H + 1) REP(j, H + 1) {
    mii(dp[i + 1][j], dp[i][j] + v[i + 1][j]);
    mii(dp[i][j + 1], dp[i][j] + v[i][j + 1]);
  }
  return dp[H][H];
}
int main() {
  DRII(H, W);
  REP(i, H) RS(s[i]);
  int an = 0;
  REPP(i, 1, W) {
    vector<char> s1, s2;
    REP(j, H) s1.PB(s[j][i - 1]);
    REP(j, H) s2.PB(s[j][i]);
    an += solve(s1, s2);
  }
  cout << an << endl;
  return 0;
}