Language: C++

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

const int MAX_M = 1000, MAX_N = 1000;
int M, N;
string flag[MAX_M];
string Emb[2];
int table[MAX_M][MAX_N];
int res, plus, n;

bool lu(int i, int j){ return flag[i][j] == Emb[0][0]; }
bool ru(int i, int j){ return flag[i][j + 1] == Emb[0][1]; }
bool ld(int i, int j){ return flag[i + 1][j] == Emb[1][0]; }
bool rd(int i, int j){ return flag[i + 1][j + 1] == Emb[1][1]; }

int main(){
	for(int i = 0; i < MAX_M; i++){
		for(int j = 0; j < MAX_N; j++){
			table[i][j] = 0;
		}
	}
	
	cin >> M >> N;
	for(int i = 0; i < M; i++){
		cin >> flag[i];
	}
	for(int i = 0; i < 2; i++){
		cin >> Emb[i];
	}
	
	res = 0;
	for(int i = 0; i < M - 1; i++){
		for(int j = 0; j < N - 1; j++){
			if(lu(i, j) && ru(i, j) && ld(i, j) && rd(i, j)) res++;
			else{
				if(ru(i, j) && ld(i, j) && rd(i, j)) table[i][j] = 1;
				else if(lu(i, j) && ld(i, j) && rd(i, j)) table[i][j] = 2;
				else if(lu(i, j) && ru(i, j) && rd(i, j)) table[i][j] = 3;
				else if(lu(i, j) && ru(i, j) && ld(i, j)) table[i][j] = 4;
				else table[i][j] = -1;
			}
		}
	}
	
	plus = 0;
	for(int i = 0; i < M - 1; i++){
		for(int j = 0; j < N - 1; j++){
			n = 0;
			if(table[i][j] == 0 || table[i][j] == -1) continue;
			if(table[i][j] == 1) n = 1;
			else if(table[i][j] == 2){
				if(j < N - 2 && table[i][j + 1] == 1) n = 2;
				else n = 1;
			}
			else if(table[i][j] == 3){
				if(i < M - 2 && table[i + 1][j] == 1) n = 2;
				else n = 1;
			}
			else{
				n = 1;
				if(i < M - 2 && j < N - 2){
					if(table[i][j + 1] == 3) n++;
					if(table[i + 1][j] == 2) n++;
					if(table[i + 1][j + 1] == 1) n++;
				}
				else if(i < M - 2){
					if(table[i + 1][j] == 2) n++;
				}
				else if(j < N - 2){
					if(table[i][j + 1] == 3) n++;
				}
			}
			
			plus = max(plus, n);
		}
	}
	
	cout << res + plus << endl;
	
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:47:9: error: reference to 'plus' is ambiguous
   47 |         plus = 0;
      |         ^~~~
In file included from /usr/include/c++/14/string:49,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_function.h:160:12: note: candidates are: 'template<class _Tp> struct std::plus'
  160 |     struct plus;
      |            ^~~~
a.cc:11:10: note:                 'int plus'
   11 | int res, plus, n;
      |          ^~~~
a.cc:76:25: error: reference to 'plus' is ambiguous
   76 |                         plus = max(plus, n);
      |                         ^~~~
/usr/include/c++/14/bits/stl_function.h:160:12: note: candidates are: 'template<class _Tp> struct std::plus'
  160 |     struct plus;
      |            ^~~~
a.cc:11:10: note:                 'int plus'
   11 | int res, plus, n;
      |          ^~~~
a.cc:76:36: error: reference to 'plus' is ambiguous
   76 |                         plus = max(plus, n);
      |                                    ^~~~
/usr/include/c++/14/bits/stl_function.h:160:12: note: candidates are: 'template<class _Tp> struct std::plus'
  160 |     struct plus;
      |            ^~~~
a.cc:11:10: note:                 'int plus'
   11 | int res, plus, n;
      |          ^~~~
a.cc:80:23: error: reference to 'plus' is ambiguous
   80 |         cout << res + plus << endl;
      |                       ^~~~
/usr/include/c++/14/bits/stl_function.h:160:12: note: candidates are: 'template<class _Tp> struct std::plus'
  160 |     struct plus;
      |            ^~~~
a.cc:11:10: note:                 'int plus'
   11 | int res, plus, n;
      |          ^~~~