#include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

int dp[2001][2001][2];
//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt"
int main() {
	int N, M; cin >> N >> M;
	string a, b; cin >> a >> b;
	//reverse(a.begin(), a.end());
	//reverse(b.begin(), b.end());
	memset(dp, 0, sizeof(dp));
	for (int i = 0; i <= 2000; ++i) {
		for (int j = 0; j <= 2000; ++j) {
			dp[i][j][1] = 0;
			dp[i][j][0] = -1e9;
		}
	}
	int ans = 0;
	for (int i = 0; i <= N; ++i) {
		for (int j = 0; j <= M; ++j) {
			for (int k = 0; k < 2; ++k) {
				if (i != N) {
					if ((a[i] == 'I') == k) {
						dp[i + 1][j][!k] = max(dp[i + 1][j][!k], dp[i][j][k] + 1);
					}
				}
				if (j != M) {
					if ((b[j] == 'I') == k) {
						dp[i][j + 1][!k] = max(dp[i][j + 1][!k], dp[i][j][k] + 1);
					}
				}
			}
			ans = max(ans, dp[i][j][0]);
		}
	}
	if (ans&&ans % 2 == 0) {
		ans--;
	}
	cout << ans << endl;
	return 0;
}