#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }



vector<string> banpei(vector<string> vstr, char c) {
    int n = vstr.size(), m = vstr[0].size();
    string line(m+2, c);
    reverse(ALL(vstr)); vstr.push_back(line);
    reverse(ALL(vstr)); vstr.push_back(line);
    for (int i = 1; i <= n; ++i) vstr[i] = c + vstr[i] + c;
    return vstr;
}



int n, m;
string fi[1100];
string sub[2];

bool check(string fi[], int i, int j) {
    for (int p = 0; p < 2; ++p)
        for (int q = 0; q < 2; ++q)
            if (fi[i+p][j+q] != sub[p][q]) return false;
    return true;
}

int main() {
    while (cin >> n >> m) {
        for (int i = 0; i < n; ++i) {
            cin >> fi[i+1];
            fi[i+1] = " " + fi[i+1] + " ";
        }
        for (int i = 0; i < 2; ++i) cin >> sub[i];
        
        int res = 0;
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= m; ++j) 
                if (check(fi, i, j)) ++res;
        
        int Max = 0, ori = 0, tmp = 0;
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= m; ++j) {
                ori = 0;
                for (int p = -1; p <= 0; ++p)
                    for (int q = -1; q <= 0; ++q)
                        if (check(fi, i+p, j+q)) ++ori;
                
                char ct = fi[i][j];
                
                tmp = 0;
                fi[i][j] = 'J';
                for (int p = -1; p <= 0; ++p)
                    for (int q = -1; q <= 0; ++q)
                        if (check(fi, i+p, j+q)) ++tmp;
                chmax(Max, tmp-ori);
                
                tmp = 0;
                fi[i][j] = 'O';
                for (int p = -1; p <= 0; ++p)
                    for (int q = -1; q <= 0; ++q)
                        if (check(fi, i+p, j+q)) ++tmp;
                chmax(Max, tmp-ori);
                
                tmp = 0;
                fi[i][j] = 'I';
                for (int p = -1; p <= 0; ++p)
                    for (int q = -1; q <= 0; ++q)
                        if (check(fi, i+p, j+q)) ++tmp;
                chmax(Max, tmp-ori);
                
                fi[i][j] = ct;
            }
    
        cout << res + Max << endl;
    }
    return 0;
}


Measured memory_bytes: 3216