import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.container;

void main(){
    while(!stdin.eof){
        auto arr = readln.chomp.split.map!(to!int).array;
        int a = arr[0], b = arr[1];
        auto s = readln.chomp;
        auto t = readln.chomp;
        auto dp = new int[][][](a+2, b+2, 2);
        foreach(i; 0..a+1) foreach(j; 0..b+1) dp[i][j][0] = dp[i][j][1] = -10;
        foreach(i; 0..a+1){
            foreach(j; 0..b+1){
                if(i != a){
                    if(s[i] == 'I') dp[i+1][j][1] = max(dp[i+1][j][1], dp[i][j][0] + 1, 1);
                    else            dp[i+1][j][0] = max(dp[i+1][j][0], dp[i][j][1] + 1);
                }
                if(j != b){
                    if(t[j] == 'I') dp[i][j+1][1] = max(dp[i][j+1][1], dp[i][j][0] + 1, 1);
                    else            dp[i][j+1][0] = max(dp[i][j+1][0], dp[i][j][1] + 1);
                }
            }
        }
        int ans = int.min;
        foreach(i; 0..a+1) foreach(j; 0..b+1) ans = max(ans, dp[i][j][1]);
        writeln(ans);
    }
}