Language: C++

#include "bits/stdc++.h"
using namespace std;

//諸機能
#pragma region MACRO 
#define putans(x)  std::cerr << "[ answer ]: " ; cout << (x) << endl
#define dputans(x) std::cerr << "[ answer ]: "; cout << setprecision(27) << (double)(x) << endl
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define RREP(i,a,n) for(int i=(int)(n-1); i>= a; i--)
#define rep(i,n) REP(i,0,n)
#define rrep(i,n) RREP(i,0,n)
#define all(a) begin((a)),end((a))
#define mp make_pair
#define exist(container, n) ((container).find((n)) != (container).end())
#define equals(a,b) (fabs((a)-(b)) < EPS)
#ifdef _DEBUG //ファイルからテストデータを読み込む
std::ifstream ifs("data.txt");
#define put ifs >>
#else //ジャッジシステムでいい感じにやる
#define put cin >>
#endif
#pragma endregion
//デバッグなどの支援
#pragma region CODING_SUPPORT
#ifdef _DEBUG
#define dbg(var0) { std::cerr << ( #var0 ) << "=" << ( var0 ) << endl; }
#define dbg2(var0, var1) { std::cerr << ( #var0 ) << "=" << ( var0 ) << ", "; dbg(var1); }
#define dbg3(var0, var1, var2) { std::cerr << ( #var0 ) << "=" << ( var0 ) << ", "; dbg2(var1, var2); }
#define dbgArray(a,n) {std::cerr << (#a) << "=";  rep(i,n){std::cerr <<(a[i])<<",";} cerr<<endl;} 
#else
#define dbg(var0) {}
#define dbg2(var0, var1) {}
#define dbg3(var0, var1, var2) {}
#define dbgArray(a,n) {}
#endif 
#pragma endregion 
//typedef（書き換える、書き足す可能性ある）
#pragma region TYPE_DEF
typedef long long ll;
typedef pair<int, int> pii; typedef pair<string, string> pss; typedef pair<int, string>pis;
typedef vector<string> vs; typedef vector<int> vi;
#pragma endregion
//諸々の定数(書き換える可能性ある)
#pragma region CONST_VAL
#define PI (2*acos(0.0))
#define EPS (1e-10)
#define MOD (ll)(1e9 + 7)
#define INF (ll)(2*1e9)
#pragma endregion


int matchab[300][300];
int matchbc[300][300];
int dp[301][301][301];

void dfs(int x, int y, int z) {
	if (x == 0 && y == 0 && z == 0) return;
	if (dp[x][y][z] == -10) return;
	dfs(x - 1, y, z);
	dfs(x, y - 1, z);
	dfs(x, y, z - 1);
	dp[x][y][z] =
		min(min(dp[x - 1][y][z] + matchab[x-1][y],
				dp[x][y-1][z] + matchab[x][y-1] + matchbc[y-1][x])
			, dp[x][y][z-1] + matchbc[y][z-1]);
}

int main() {
	int h, w; put h >> w;
	vector<string> table;
	rep(i, h) {
		string tmp; put tmp;
		table.push_back(tmp);
	}
	if (w > 4) {
		return 0;
	}

	char a[3][300];//転置
	rep(i, 3) {
		rep(j, h) {
			a[i][j] = table[h-1-j][i];
		}
	}
	dp[0][0][0] = 0;
	dp[1][0][0] = dp[0][1][0] = dp[0][0][1] = 0;
	fill(*dp, *dp + 301 * 301 * 301, -10);
	fill(*matchab, *matchab + 300 * 300, 0);
	fill(*matchbc, *matchbc + 300 * 300, 0);
	REP(i,0, h) {
		rep(j,h) {
			rep(k, min(h-i,h-j)) {
				if (a[0][k + i] == a[1][k + j]) {
					matchab[i][j]++;
				}
			}
		}
	}
	REP(i, 0, h) {
		rep(j, h) {
			rep(k, min(h - i, h - j)) {
				if (a[1][k + i] == a[2][k + j]) {
					matchbc[i][j]++;
				}
			}
		}
	}
	dfs(h, h, h);
	putans(dp[2][2][2]);
END: 
	return 0;
}



//
//vi v;
//vi w; ;
//ll nz1() {
//	return 0;
//}
//
//ll nz2() {
//	ll dp[201][1000*200+1];
//
//}
//
//ll nz3() {
//
//}
//	int n, wkazu; put n >> wkazu;
//	v.resize(200); 
//	w.resize(200);
//	rep(i, n) {
//		put v[i] >> w[i];
//	}

Compiler output:
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h: In instantiation of 'typename __gnu_cxx::__enable_if<std::__is_scalar<_Tp>::__value, void>::__type std::__fill_a1(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = int (*)[301]; _Tp = int; typename __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type = void]':
/usr/include/c++/14/bits/stl_algobase.h:998:21:   required from 'void std::__fill_a(_FIte, _FIte, const _Tp&) [with _FIte = int (*)[301]; _Tp = int]'
  998 |     { std::__fill_a1(__first, __last, __value); }
      |       ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:1029:20:   required from 'void std::fill(_ForwardIterator, _ForwardIterator, const _Tp&) [with _ForwardIterator = int (*)[301]; _Tp = int]'
 1029 |       std::__fill_a(__first, __last, __value);
      |       ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:87:6:   required from here
   87 |         fill(*dp, *dp + 301 * 301 * 301, -10);
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:952:18: error: incompatible types in assignment of 'const int' to 'int [301]'
  952 |         *__first = __tmp;
      |         ~~~~~~~~~^~~~~~~