Language: C++

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

const string JOI = "JOI";

int main()
{
	int H, W;

	scanf("%d", &H);
	scanf("%d", &W);

	vector<string> D(H);

	for (int i = 0; i < H; i++)
	{
		cin >> D[i];
	}

	string C1, C2; cin >> C1 >> C2;

	int sum = 0;

	for (int i = 0; i < H - 1; i++)
	{
		for (int j = 0; j < W - 1; j++)
		{
			string R1 = D[i].substr(j, 2);
			string R2 = D[i + 1].substr(j, 2);

			if (R1 == C1 && R2 == C2)
			{
				sum++;
			}
		}
	}

	int diff, ret = sum;

	for (int i = 0; i < H; i++)
	{
		for (int j = 0; j < W; j++)
		{
			diff = 0; char prevch = D[i][j];

			for (int k = 0; k < 3; k++)
			{
				if (JOI[k] != prevch)
				{
					if (i != 0 && j != 0)
					{
						D[i][j] = JOI[k];

						string R1 = D[i - 1].substr(j - 1, 2);
						string R2 = D[i].substr(j - 1, 2);

						diff += (R1 == C1 && R2 == C2 ? 1 : 0);

						D[i][j] = prevch;

						string R1 = D[i - 1].substr(j - 1, 2);
						string R2 = D[i].substr(j - 1, 2);

						diff -= (R1 == C1 && R2 == C2 ? 1 : 0);
					}

					if (i != 0 && j + 1 != W)
					{
						D[i][j] = JOI[k];

						string R1 = D[i - 1].substr(j, 2);
						string R2 = D[i].substr(j, 2);

						diff += (R1 == C1 && R2 == C2 ? 1 : 0);

						D[i][j] = prevch;

						string R1 = D[i - 1].substr(j, 2);
						string R2 = D[i].substr(j, 2);

						diff -= (R1 == C1 && R2 == C2 ? 1 : 0);
					}

					if (i + 1 != H && j != 0)
					{
						D[i][j] = JOI[k];

						string R1 = D[i].substr(j - 1, 2);
						string R2 = D[i + 1].substr(j - 1, 2);

						diff += (R1 == C1 && R2 == C2 ? 1 : 0);

						D[i][j] = prevch;

						string R1 = D[i].substr(j - 1, 2);
						string R2 = D[i + 1].substr(j - 1, 2);

						diff -= (R1 == C1 && R2 == C2 ? 1 : 0);
					}

					if (i + 1 != H && j + 1 != W)
					{
						D[i][j] = JOI[k];

						string R1 = D[i].substr(j, 2);
						string R2 = D[i + 1].substr(j, 2);

						diff += (R1 == C1 && R2 == C2 ? 1 : 0);

						D[i][j] = prevch;

						string R1 = D[i].substr(j, 2);
						string R2 = D[i + 1].substr(j, 2);

						diff -= (R1 == C1 && R2 == C2 ? 1 : 0);
					}
				}

				ret = max(ret, sum + diff);
			}

			D[i][j] = prevch;
		}
	}

	printf("%d\n", ret);

	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:65:56: error: redeclaration of 'std::string R1'
   65 |                                                 string R1 = D[i - 1].substr(j - 1, 2);
      |                                                        ^~
a.cc:58:56: note: 'std::string R1' previously declared here
   58 |                                                 string R1 = D[i - 1].substr(j - 1, 2);
      |                                                        ^~
a.cc:66:56: error: redeclaration of 'std::string R2'
   66 |                                                 string R2 = D[i].substr(j - 1, 2);
      |                                                        ^~
a.cc:59:56: note: 'std::string R2' previously declared here
   59 |                                                 string R2 = D[i].substr(j - 1, 2);
      |                                                        ^~
a.cc:82:56: error: redeclaration of 'std::string R1'
   82 |                                                 string R1 = D[i - 1].substr(j, 2);
      |                                                        ^~
a.cc:75:56: note: 'std::string R1' previously declared here
   75 |                                                 string R1 = D[i - 1].substr(j, 2);
      |                                                        ^~
a.cc:83:56: error: redeclaration of 'std::string R2'
   83 |                                                 string R2 = D[i].substr(j, 2);
      |                                                        ^~
a.cc:76:56: note: 'std::string R2' previously declared here
   76 |                                                 string R2 = D[i].substr(j, 2);
      |                                                        ^~
a.cc:99:56: error: redeclaration of 'std::string R1'
   99 |                                                 string R1 = D[i].substr(j - 1, 2);
      |                                                        ^~
a.cc:92:56: note: 'std::string R1' previously declared here
   92 |                                                 string R1 = D[i].substr(j - 1, 2);
      |                                                        ^~
a.cc:100:56: error: redeclaration of 'std::string R2'
  100 |                                                 string R2 = D[i + 1].substr(j - 1, 2);
      |                                                        ^~
a.cc:93:56: note: 'std::string R2' previously declared here
   93 |                                                 string R2 = D[i + 1].substr(j - 1, 2);
      |                                                        ^~
a.cc:116:56: error: redeclaration of 'std::string R1'
  116 |                                                 string R1 = D[i].substr(j, 2);
      |                                                        ^~
a.cc:109:56: note: 'std::string R1' previously declared here
  109 |                                                 string R1 = D[i].substr(j, 2);
      |                                                        ^~
a.cc:117:56: error: redeclaration of 'std::string R2'
  117 |                                                 string R2 = D[i + 1].substr(j, 2);
      |                                                        ^~
a.cc:110:56: note: 'std::string R2' previously declared here
  110 |                                                 string R2 = D[i + 1].substr(j, 2);
      |                                                        ^~