import core.stdc.stdio;
import std.algorithm;

void main(){
	int s,t;
	scanf("%d%d",&s,&t);
	char[] x = new char[s+1],y=new char[t+1];
	scanf("%s%s",x.ptr,y.ptr);
	int[][][] dp = new int[][][](2,s+1,t+1);
	foreach(d;dp[0])
		d[]=-1145141919;
	foreach(i;0..s+1)
		foreach(j;0..t+1){
			if(i<s)
				if(x[i]=='I')
					dp[0][i+1][j]=max(dp[0][i+1][j],dp[1][i][j]+1);
				else
					dp[1][i+1][j]=max(dp[1][i+1][j],dp[0][i][j]+1);
			if(j<t)
				if(y[j]=='I')
					dp[0][i][j+1]=max(dp[0][i][j+1],dp[1][i][j]+1);
				else
					dp[1][i][j+1]=max(dp[1][i][j+1],dp[0][i][j]+1);
		}
	int ans=0;
	foreach(ds;dp[0])
		foreach(d;ds)
			ans=max(ans,d);
	printf("%d\n",ans);
}