//#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
const int INF = (1LL<<30);
const ll LLINF = (1LL<<60);
const double PI = 3.14159265359;
const double EPS = 1e-12;
const int MOD = 1000000007;
//#define int ll

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

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

int n, m, dp[2010][2010][2], s[2010], t[2010];
string ss, tt;
signed main(void)
{
  cin >> n >> m >> ss >> tt;
  REP(i, n) s[i] = ss[i]=='I'?0:1;
  REP(i, m) t[i] = tt[i]=='I'?0:1;
  REP(i, n) if(!s[i] || dp[i][0][0]) {
    chmax(dp[i+1][0][s[i]], dp[i][0][!s[i]]+1);
  }
  REP(i, m) if(!t[i] || dp[0][i][0]) {
    chmax(dp[0][i+1][t[i]], dp[0][i][!t[i]]+1);
  }
  REP(i, n) REP(j, m) {
    if(!s[i] || dp[i][j+1][0]) chmax(dp[i+1][j+1][s[i]], dp[i][j+1][!s[i]]+1);
    if(!t[j] || dp[i+1][j][0]) chmax(dp[i+1][j+1][t[j]], dp[i+1][j][!t[j]]+1);
  }
  int ma = 0;
  REP(i, n+1) {
    REP(j, m+1) {
      chmax(ma, dp[i][j][0]);
      // cout << dp[i][j][0] << "," << dp[i][j][1] << " ";
    }
    // cout << endl;
  }
  cout << ma << endl;

  return 0;
}