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


int main(){
    int m, n; cin >> m >> n;

    vector<vector<char> > a(m + 2, vector<char> (n + 2, 'd'));
    for(int i = 1; i <= m; i++){
        for(int j = 1; j <= n; j++){
            cin >> a[i][j];
        }
    }

    vector<char> b(4);
    for(int i = 0; i < 4; i++) cin >> b[i];

    int sum = 0;
    for(int i = 1; i <= m; i++){
        for(int j = 1; j <= n; j++){
            if(a[i][j] == b[0] && a[i][j + 1] == b[1] && a[i + 1][j] == b[2] && a[i + 1][j + 1] == b[3]) sum++;
        }
    }

    //cout << sum << endl;
    int ans = sum;

    for(int i = 1; i <= m; i++){
        for(int j = 1; j <= n; j++){
            int temp = sum;
            char c = a[i][j];
            temp -= (a[i - 1][j - 1] == b[0] && a[i - 1][j] == b[1] && a[i][j - 1] == b[2] && a[i][j] == b[3]);
            //if(i == 2 && j == 2) cout << temp << endl;
            temp -= (a[i - 1][j] == b[0] && a[i - 1][j + 1] == b[1] && a[i][j] == b[2] && a[i][j + 1] == b[3]);
            //if(i == 2 && j == 2) cout << temp << endl;            
            temp -= (a[i][j - 1] == b[0] && a[i][j] == b[1] && a[i + 1][j - 1] == b[2] && a[i + 1][j] == b[3]);
            //if(i == 2 && j == 2) cout << temp << endl;            
            temp -= (a[i][j] == b[0] && a[i][j + 1] == b[1] && a[i + 1][j] == b[2] && a[i + 1][j + 1] == b[3]);
            //if(i == 2 && j == 2) cout << temp << endl;
            
            //if(i == 2 && j == 2) cout << i << " " << j << " " << temp << endl;
            if(c != 'I'){
                a[i][j] = 'I';
                int tmp = temp;
                tmp += (a[i - 1][j - 1] == b[0] && a[i - 1][j] == b[1] && a[i][j - 1] == b[2] && a[i][j] == b[3]);
                tmp += (a[i - 1][j] == b[0] && a[i - 1][j + 1] == b[1] && a[i][j] == b[2] && a[i][j + 1] == b[3]);
                tmp += (a[i][j - 1] == b[0] && a[i][j] == b[1] && a[i + 1][j - 1] == b[2] && a[i + 1][j] == b[3]);
                tmp += (a[i][j] == b[0] && a[i][j + 1] == b[1] && a[i + 1][j] == b[2] && a[i + 1][j + 1] == b[3]);
                ans = max(ans, tmp);
            }

            if(c != 'O'){
                a[i][j] = 'O';
                int tmp = temp;
                tmp += (a[i - 1][j - 1] == b[0] && a[i - 1][j] == b[1] && a[i][j - 1] == b[2] && a[i][j] == b[3]);
                tmp += (a[i - 1][j] == b[0] && a[i - 1][j + 1] == b[1] && a[i][j] == b[2] && a[i][j + 1] == b[3]);
                tmp += (a[i][j - 1] == b[0] && a[i][j] == b[1] && a[i + 1][j - 1] == b[2] && a[i + 1][j] == b[3]);
                tmp += (a[i][j] == b[0] && a[i][j + 1] == b[1] && a[i + 1][j] == b[2] && a[i + 1][j + 1] == b[3]);
                ans = max(ans, tmp);                 
            }

            if(c != 'J'){
                a[i][j] = 'J';
                int tmp = temp;
                tmp += (a[i - 1][j - 1] == b[0] && a[i - 1][j] == b[1] && a[i][j - 1] == b[2] && a[i][j] == b[3]);
                tmp += (a[i - 1][j] == b[0] && a[i - 1][j + 1] == b[1] && a[i][j] == b[2] && a[i][j + 1] == b[3]);
                tmp += (a[i][j - 1] == b[0] && a[i][j] == b[1] && a[i + 1][j - 1] == b[2] && a[i + 1][j] == b[3]);
                tmp += (a[i][j] == b[0] && a[i][j + 1] == b[1] && a[i + 1][j] == b[2] && a[i + 1][j + 1] == b[3]);
                ans = max(ans, tmp);                 
            }

            a[i][j] = c;
        }
    }

    cout << ans << endl;
}


Measured memory_bytes: 3920