#include <bits/stdc++.h>
using namespace std;
int n, m, ans;
bool get1[2001], get2[2001];
string s, t;
int dp[2][2001][2001];
int main()
{
	cin >> n >> m >> s >> t;
	memset(dp[1], -1, sizeof(dp[1]));
	for(int i = 0; i < n; ++i)
		get1[i] = (s[i] == 'I');
	for(int i = 0; i < m; ++i)
		get2[i] = (t[i] == 'I');
	for(int i = 0; i <= n; ++i)
	{
		for(int j = 0; j <= m; ++j)
		{
			if(i != n)
				dp[get1[i]][i + 1][j] = max(dp[get1[i]][i + 1][j], dp[get1[i] ^ 1][i][j] + 1);
			if(j != m)
				dp[get2[j]][i][j + 1] = max(dp[get2[j]][i][j + 1], dp[get2[j] ^ 1][i][j] + 1);
			ans = max(ans, dp[1][i][j]);
		}
	}
	cout << ans << endl;
}