import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.range;
import std.container;
 
void main(){
    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[][][](2, a+2, b+2);
    foreach(i; 0..a+1) foreach(j; 0..b+1) dp[0][i][j] = dp[1][i][j] = int.min;
    foreach(i; 0..a+1){
        foreach(j; 0..b+1){
            if(i != a){
                if(s[i] == 'I') dp[1][i+1][j] = max(dp[1][i+1][j], dp[0][i][j] + 1, 1);
                else            dp[0][i+1][j] = max(dp[0][i+1][j], dp[1][i][j] + 1);
            }
            if(j != b){
                if(t[j] == 'I') dp[1][i][j+1] = max(dp[1][i][j+1], dp[0][i][j] + 1, 1);
                else            dp[0][i][j+1] = max(dp[0][i][j+1], dp[1][i][j] + 1);
            }
        }
    }
    int ans = int.min;
    foreach(i; 0..a+2) foreach(j; 0..b+2) ans = max(ans, dp[0][i][j], dp[1][i][j]);
    writeln(ans);
}