#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
const int INF=1001001001;
char S[2002],T[2002];
int M,N;
int dp[2002][2002][2];//[a][b][c] S???a,T???b?????§?????£?????¨????????????????????????????????????c(0=I,1=O)??§????????¨?????????????????????
class IOItrain{
    public:
    void initialize(){
        fill(dp[0][0],dp[2002][0],0);
    }
    int solve(){
        int ans=0;
        scanf("%d %d",&M,&N);
        scanf("%s",S);
        scanf("%s",T);
        rep(i,M+1){
            rep(j,N+1){
                if(i==0&&j==0){
                    continue;//???????????£?????????????????§0
                }
                if(i==0){
                    dp[i][j][0]=( T[j-1]=='I' ? dp[i][j-1][1] : -1)+1;
                    dp[i][j][1]=( T[j-1]=='O'&&dp[i][j-1][0]>0 ? dp[i][j-1][0] : -1)+1;
                }
                if(j==0){
                    dp[i][j][0]= (S[i-1]=='I' ? dp[i-1][j][1] : -1)+1;
                    dp[i][j][1]= (S[i-1]=='O'&&dp[i-1][j][1]>0 ? dp[i-1][j][1] : -1)+1;
                }
                if(i!=0&&j!=0){
                    dp[i][j][0]=max( (S[i-1]=='I' ? dp[i-1][j][1] : -1) , ( T[j-1]=='I' ? dp[i][j-1][1] : -1) )+1;
                    dp[i][j][1]=max( (S[i-1]=='O'&&dp[i-1][j][0]>0 ? dp[i-1][j][0] : -1) , ( T[j-1]=='O'&&dp[i][j-1][0]>0 ? dp[i][j-1][0] : -1) )+1;
                }
                ans=max(ans,dp[i][j][0]);
            }
        }
        
        return ans;//-(ans%2 ? 1 : 0 );//??¶??°?????????1?????????????\???°?????????(I??§???????????????)
    }
};
signed main(){
    IOItrain ans;
    ans.initialize();
    printf("%d\n",ans.solve());
    return 0;
}