#include <string>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <iomanip>

#include <iostream>

using namespace std;

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(ll i = m;i < n;i++)
#define FORR(i, m, n) for(ll i = m;i >= n;i--)
#define REPO(i, n) for(int i = 1;i <= n;i++)
#define ll long long
#define INF 1999999999
#define MINF -1999999999
#define INF64 1999999999999999999
#define ALL(n) n.begin(),n.end()
#define MOD 1000000007

ll n, m, dp[2100][2100][2], ans = 0;
bool a[2100], b[2100];
string aa, bb;
int main() {
	cin >> n >> m;
	cin >> aa >> bb;
	REP(i, n)if (aa[i] == 'I')a[i] = true;
	REP(i, m)if (bb[i] == 'I')b[i] = true;
	REP(i, n + 1) {
		REP(j, m + 1) {
			REP(k, 2) {
				if (k != a[i])dp[i + 1][j][(k + 1) % 2] = max(dp[i + 1][j][(k + 1) % 2], dp[i][j][k] + (k + 1) % 2);
				if (k != b[j])dp[i][j + 1][(k + 1) % 2] = max(dp[i][j + 1][(k + 1) % 2], dp[i][j][k] + (k + 1) % 2);
			}
		}
	}
	REP(i, n + 1) {
		REP(j, m + 1) {
			ans = max(ans, dp[i][j][1]);
		}
	}
	if (ans == 0)cout << ans << endl;
	else cout << ans * 2 - 1 << endl;
}

