import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

void main() {
    int n,m;
    char[] s, t;
    scan(n,m);
    s = readln.chomp.to!(char[]);
    t = readln.chomp.to!(char[]);
    s ~= '#';
    t ~= '#';
    s.reverse();
    t.reverse();

    auto dpi = new int[][](n + 1, m + 1);
    auto dpo = new int[][](n + 1, m + 1);

    foreach (i ; 1 .. n + 1) {
        if (s[i] == 'I') {
            dpi[i][0] = dpo[i-1][0] + 1;
        }
        if (s[i] == 'O') {
            dpo[i][0] = dpi[i-1][0] + 1;
        }
    }

    foreach (j ; 1 .. m + 1) {
        if (t[j] == 'I') {
            dpi[0][j] = dpo[0][j-1] + 1;
        }
        if (t[j] == 'O') {
            dpo[0][j] = dpi[0][j-1] + 1;
        }
    }

    foreach (i ; 1 .. n + 1) {
        foreach (j ; 1 .. m + 1) {
            if (s[i] == 'I') {
                dpi[i][j] = max(dpi[i][j], dpo[i-1][j] + 1);
            }
            else {
                dpo[i][j] = max(dpo[i][j], dpi[i-1][j] + 1);
            }

            if (t[j] == 'I') {
                dpi[i][j] = max(dpi[i][j], dpo[i][j-1] + 1);
            }
            else {
                dpo[i][j] = max(dpo[i][j], dpi[i][j-1] + 1);
            }
        }
    }

    debug {
        writefln("%(%(%s %)\n%)", dpi);
        writeln;
        writefln("%(%(%s %)\n%)", dpo);
    }

    int ans;

    foreach (i ; 0 .. n + 1) {
        foreach (j ; 0 .. m + 1) {
            if (dpi[i][j] & 1) {
                ans = max(ans, dpi[i][j]);
            }
        }
    }

    writeln(ans);
}


void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}


void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}
