Language: C++

#include <bits/stdc++.h>

using namespace std;

int d[301][301][301];

int onetwo[300][300];
int twothr[300][300];
int onethr[300][300];

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    int h, w;
    cin >> h >> w;

    if (w == 3) {
        int n = h;
        string a(n, '0'), b(n, '0'), c(n, '0');

        for (int i = 0; i < n; i++) {
            if (w == 3) {
                cin >> a[i] >> b[i] >> c[i];
            } else if (w == 2) {
                cin >> a[i] >> b[i];
            } else {
                cin >> a[i];
            }
        }
        reverse(a.begin(), a.end());
        reverse(b.begin(), b.end());
        reverse(c.begin(), c.end());

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int ii = i, jj = j;

                for (int k = min(i, j); k >= 0; k--) {
                    onetwo[i][j] += (a[ii] == b[jj]);
                    ii--;
                    jj--;
                }
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int ii = i, jj = j;

                for (int k = min(i, j); k >= 0; k--) {
                    twothr[i][j] += (b[ii] == c[jj]);
                    ii--;
                    jj--;
                }
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                int ii = i, jj = j;

                for (int k = min(i, j); k >= 0; k--) {
                    onethr[i][j] += (a[ii] == c[jj]);
                    ii--;
                    jj--;
                }
            }
        }

        for (int i = 0; i <= n; i++) {
            for (int j = 0; j <= n; j++) {
                for (int k = 0; k <= n; k++) {
                    if (i == 0 && j == 0 && k == 0) {
                        continue;
                    }
                    d[i][j][k] = 1000 * 1000;

                    if (i > 0) {
                        d[i][j][k] = min(d[i][j][k], d[i - 1][j][k] + (j == 0 ? 0 : onetwo[i - 1][j - 1]);
                    }
                    if (k > 0) {
                        d[i][j][k] = min(d[i][j][k], d[i][j][k - 1] + (j == 0 ? 0 : twothr[j - 1][k - 1]);
                    }
                    if (j > 0) {
                        d[i][j][k] = min(d[i][j][k], d[i][j - 1][k] + (i == 0 ? 0 : onetwo[i - 1][j - 1]) + (k == 0 ? 0 : twothr[j - 1][k - 1]));
                    }
                }
            }
        }
        cout << d[n][n][n] << '\n';
    } else {
        throw;
    }

    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:81:106: error: expected ')' before ';' token
   81 |                         d[i][j][k] = min(d[i][j][k], d[i - 1][j][k] + (j == 0 ? 0 : onetwo[i - 1][j - 1]);
      |                                         ~                                                                ^
      |                                                                                                          )
a.cc:84:106: error: expected ')' before ';' token
   84 |                         d[i][j][k] = min(d[i][j][k], d[i][j][k - 1] + (j == 0 ? 0 : twothr[j - 1][k - 1]);
      |                                         ~                                                                ^
      |                                                                                                          )