#include<cstdio>
#include<algorithm>
#include<functional>
#include<cstring>
#include<iostream>
#include<cmath>
#include<queue>
#include<cctype>
#include<string>
#define rep(i,a,b) for(i=a;i<=b;i++)
using namespace std;
int dp[2001][2001][2];
int main(void)
{
	int i,j,k,n,m;
	char s[2001],t[2001];
	cin>>m>>n>>s>>t;
	rep(i,0,m)rep(j,0,n) 
		dp[i][j][0]=-1,dp[i][j][1]=-1;
	if(s[0]=='I') dp[1][0][1]=1;
	else dp[1][0][0]=0;
	if(t[0]=='I') dp[0][1][1]=1;
	else dp[0][1][0]=0;
	rep(i,0,m) {
		rep(j,0,n) {
			if(i<m) {
				if(s[i]=='I') 
					dp[i+1][j][1]=max(1,max(dp[i][j][0]+1,dp[i+1][j][1]));
				else 
					dp[i+1][j][0]=max(0,max(dp[i][j][1]+1,dp[i+1][j][0]));
			}
			if(j<n) {
				if(t[j]=='I') 
					dp[i][j+1][1]=max(1,max(dp[i][j][0]+1,dp[i][j+1][1]));
				else 	
					dp[i][j+1][0]=max(0,max(dp[i][j][1]+1,dp[i][j+1][0]));
			}
		}
	}
	int mx;
	rep(i,0,m)rep(j,0,n) mx=max(mx,max(dp[i][j][0],dp[i][j][1]));
	if(mx==-1)mx=0;
	else if(mx%2==0 && mx!=0) mx--;
	cout<<mx<<endl;
	return 0;
}
	
	
	
	