//#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a<=x&&x<b)
#define MP make_pair
#define PB push_back
#define INF (1LL<<30)
#define LLINF (1LL<<60)
#define PI 3.14159265359
#define EPS 1e-12
//#define int ll

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

string s[1010], t[2];
int dp[1010][1010];
signed main(void)
{
  int h, w;
  cin >> h >> w;
  REP(i, h) cin >> s[i];
  REP(i, 2) cin >> t[i];

  int sum = 0;
  FOR(i, 1, h) FOR(j, 1, w) {
    if(s[i-1][j-1] == t[0][0] && s[i-1][j] == t[0][1] && s[i][j-1] == t[1][0] && s[i][j] == t[1][1]) {
      dp[i][j] = 1;
      sum++;
    }
  }

  char c[3] = {'J', 'O', 'I'};
  int ans = 0;
  REP(i, h) REP(j, w) REP(k, 3) {
    int ret = sum;
    // ??????
    if(i != 0 && j != 0) {
      // if(!i && !j && k == 2) cout << "a" << ret << endl;
      if(dp[i][j]) ret--;
      if(s[i-1][j-1] == t[0][0] && s[i-1][j] == t[0][1] && s[i][j-1] == t[1][0] && c[k] == t[1][1]) {
        ret++;
      }
    }
    // ??????
    if(i != 0 && j != w-1) {
      if(dp[i][j+1]) ret--;
      if(s[i-1][j] == t[0][0] && s[i-1][j+1] == t[0][1] && c[k] == t[1][0] && s[i][j+1] == t[1][1]) {
        ret++;
      }
    }
    // ??????
    if(i != h-1 && j != 0) {
      if(dp[i+1][j]) ret--;
      if(s[i][j-1] == t[0][0] && c[k] == t[0][1] && s[i+1][j-1] == t[1][0] && s[i+1][j] == t[1][1]) {
        ret++;
      }
    }
    // ??????
    if(i != h-1 && j != w-1) {
      if(dp[i+1][j+1]) ret--;
      if(c[k] == t[0][0] && s[i][j+1] == t[0][1] && s[i+1][j] == t[1][0] && s[i+1][j+1] == t[1][1]) {
        ret++;
      }
    }
    // if(!i && !j && k == 2) cout << ret << endl;
    ans = max(ans, ret);
  }
  cout << ans << endl;
  //
  // FOR(i, 1, h) FOR(j, 1, w) {
  //   dp[i][j][0] = dp[i-1][j][0] + dp[i][j-1][0] - dp[i-1][j-1][0];
  //   if(s[i-1][j-1] == t[0][0] && s[i-1][j] == t[0][1] && s[i][j-1] == t[1][0] && s[i][j] == t[1][1]) {
  //     dp[i][j][0]++;
  //   }
  //   dp[i][j][1] = dp[i][j][0];
  //   /*if(s[i-1][j-1] == t[0][0] && s[i-1][j] == t[0][1] && s[i][j-1] == t[1][0] && s[i][j] == t[1][1]) {
  //     dp[i][j][1]++;
  //   }*/
  //   if((s[i-1][j-1] != t[0][0] && s[i-1][j] == t[0][1] && s[i][j-1] == t[1][0] && s[i][j] == t[1][1]) || (s[i-1][j-1] == t[0][0] && s[i-1][j] != t[0][1] && s[i][j-1] == t[1][0] && s[i][j] == t[1][1]) || (s[i-1][j-1] == t[0][0] && s[i-1][j] == t[0][1] && s[i][j-1] != t[1][0] && s[i][j] == t[1][1]) || (s[i-1][j-1] == t[0][0] && s[i-1][j] == t[0][1] && s[i][j-1] == t[1][0] && s[i][j] != t[1][1])) {
  //     dp[i][j][1] = dp[i-1][j][0] + dp[i][j-1][0] - dp[i-1][j-1][0] + 1;
  //   }
  //   int tmp = dp[i-1][j][1] + dp[i][j-1][1] - dp[i-1][j-1][1];
  //   if(s[i-1][j-1] == t[0][0] && s[i-1][j] == t[0][1] && s[i][j-1] == t[1][0] && s[i][j] == t[1][1]) tmp++;
  //   dp[i][j][1] = max(dp[i][j][1], tmp);
  // }
  //
  // REP(i, h) {
  //   REP(j, w) {
  //     cout << "(" << dp[i][j][0] << "," << dp[i][j][1] << ")";
  //   }
  //   cout << endl;
  // // }
  //
  // cout << max(dp[h-1][w-1][0], dp[h-1][w-1][1]) << endl;

  return 0;
}

Measured memory_bytes: 8008