#include <iostream>
#include <string>
#include <vector>

using namespace std;

string s, t;
vector<vector<vector<int>>> dp;

int calc(int x, int y, bool isI) {
    int ret = 0;
    if (x < s.size() && ((s[x] == 'I') == isI)) {
        if (dp[x + 1][y][!isI] < 0) dp[x + 1][y][!isI] = calc(x + 1, y, !isI) + 1;
        ret = max(ret, dp[x + 1][y][!isI]);
    }
    if (y < t.size() && ((t[y] == 'I') == isI)) {
        if (dp[x][y + 1][!isI] < 0)dp[x][y + 1][!isI] = calc(x, y + 1, !isI) + 1;
        ret = max(ret, dp[x][y + 1][!isI]);
    }
    return ret;
}

int main() {
    int m, n;
    cin >> m >> n >> s >> t;

    int ret = 0;
    dp = vector<vector<vector<int>>>(m + 1, vector<vector<int>>(n + 1, vector<int>(2, -1)));
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            ret = max(ret, (calc(i, j, true) + 1) / 2 * 2 - 1);
        }
    }

    cout << ret << endl;
    return 0;
}
