#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<numeric>
using namespace std;
//const int dx[]={1,0,-1,0};
//const int dy[]={0,1,0,-1};
#define INF 1e+8
#define EPS 1e-7
#define PB push_back
#define fi first
#define se second
#define ll long long
#define reps(i,j,k) for(int i = (j); i < (k); i++)
#define rep(i,j) reps(i,0,j)
typedef complex < double > Point;
typedef pair<int,int> Pii;
typedef vector < int > vi;
typedef vector < vi > vii;
const int dx[]={0,1,0,1},dy[] = {0,0,1,1};
char flag[1024][1024];
bool made[1024][1024];
int check(string str,int y,int x){
    rep(i,4){
        if(flag[y+dy[i]][x+dx[i]] != str[i])return 0;
    }
    rep(i,4){
        made[y+dy[i]][x+dx[i]] = 1;
    }
    return 1;
}
int solve(int i,int j,string x,int m,int n){
    int res=0;
    if(i == 0 && j == 0){res+=check(x,i,j);}
    else if(i == 0 && j == m-1){res+=check(x,i,j-1);}
    else if(i == n-1 && j == 0){res+=check(x,i-1,j);}
    else if(i == n-1 && j == m-1){res+=check(x,i-1,j-1);}
    else if(i == 0){res+=check(x,i,j);res+=check(x,i,j-1);}
    else if(i == n-1){res+=check(x,i-1,j-1);res+=check(x,i-1,j);}
    else if(j == 0){res+=check(x,i-1,j);res+=check(x,i,j);}
    else if(j == m-1){res+=check(x,i-1,j-1);res+=check(x,i,j-1);}
    else{res+=check(x,i-1,j-1);res+=check(x,i-1,j);res+=check(x,i,j-1);res+=check(x,i,j);}
    return res;
}
int main(){
    int n,m;
    scanf("%d%d",&n,&m);
    rep(i,n){
        scanf("%s",flag[i]);
    }

    string x;
    string y;
    cin >> x;
    cin >> y;
    x += y;
    int ans = 0;
    rep(i,n){
        rep(j,m){
            if(x[0] == flag[i][j]){
                ans += check(x,i,j);
            }
        }
    }
    int ma = 0;
    rep(i,n){
        rep(j,m){
            if(!made[i][j]){
                char sub = flag[i][j];
                int res = 0;
                if(sub == 'J'){
                    int hoge1=0;
                    int hoge2=0;
                    flag[i][j] = 'O';
                    hoge1 = solve(i,j,x,m,n);
                    flag[i][j] = 'I';
                    hoge2 = solve(i,j,x,m,n);
                    flag[i][j] = 'J';
                    res += max(hoge1,hoge2);
                }
                if(sub == 'O'){
                    int hoge1=0;
                    int hoge2=0;
                    flag[i][j] = 'J';
                    hoge1 = solve(i,j,x,m,n);
                    flag[i][j] = 'I';
                    hoge2 = solve(i,j,x,m,n);
                    flag[i][j] = 'O';
                    res += max(hoge1,hoge2);
                }
                if(sub == 'I'){
                    int hoge1=0;
                    int hoge2=0;
                    flag[i][j] = 'J';
                    hoge1 = solve(i,j,x,m,n);
                    flag[i][j] = 'O';
                    hoge2 = solve(i,j,x,m,n);
                    flag[i][j] = 'I';
                    res += max(hoge1,hoge2);
                }
                ma = max(res,ma);
            }
        }
    }
    printf("%d\n",ans+ma);
    return 0;
}

Measured memory_bytes: 3244