// Original C++ solution (slower):
#include <string>

#include <vector>

#include <iostream>

#include <algorithm>



#pragma warning(disable : 4996)



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++)

		{

			char prevch = D[i][j];



			for (int k = 0; k < 3; k++)

			{

			    diff = 0;

			    

				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 R3 = D[i - 1].substr(j - 1, 2);

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

					

					diff -= (R3 == C1 && R4 == 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 R3 = D[i - 1].substr(j, 2);

					string R4 = D[i].substr(j, 2);



					diff -= (R3 == C1 && R4 == 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 R3 = D[i].substr(j - 1, 2);

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



					diff -= (R3 == C1 && R4 == 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 R3 = D[i].substr(j, 2);

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



					diff -= (R3 == C1 && R4 == C2 ? 1 : 0);

				}



				ret = max(ret, sum + diff);

			}

		}

	}



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



	return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

int H, W, bs, res, ret; char c, s[1009][1009], t[5][5], r[] = "JOI";

bool check(int x, int y) { return 0 <= x && x < H - 1 && 0 <= y && y < W - 1 && s[x][y] == t[0][0] && s[x][y + 1] == t[0][1] && s[x + 1][y] == t[1][0] && s[x + 1][y + 1] == t[1][1]; }

int main() {

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

	for(int i = 0; i < H; i++) scanf("%s", s[i]);

	scanf("%s%s", t[0], t[1]);

	for(int i = 0; i < H; i++) {

		for(int j = 0; j < W; j++) {

			bs += check(i, j); c = s[i][j];

			for(int k = 0; k < 3; k++) {

				res = -check(i - 1, j - 1) - check(i - 1, j) - check(i, j - 1) - check(i, j);

				s[i][j] = r[k];

				res += check(i - 1, j - 1) + check(i - 1, j) + check(i, j - 1) + check(i, j);

				ret = ret > res ? ret : res;

				s[i][j] = c;

			}

		}

	}

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

	return 0;

}


// Fastest known solution for this problem: