
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

enum Type{
	I,
	O,
};


int main(){

	int M,N;
	scanf("%d %d",&M,&N);

	char* S = new char[M+1];
	char* T = new char[N+1];

	scanf("%s",S);
	scanf("%s",T);

	int*** dp = new int**[M+1];
	for(int i = 0; i <= M; i++){
		dp[i] = new int*[N+1];
		for(int k = 0; k <= N; k++){
			dp[i][k] = new int[2];
			dp[i][k][I] = 0;
			dp[i][k][O] = 0;
		}
	}

	int ans = 0;

	for(int i = 0; i <= M; i++){
		for(int k = 0; k <= N; k++){
			if(i != 0){
				if(S[i-1] == 'I'){
					dp[i][k][I] = dp[i-1][k][O]+1;
				}else if(S[i-1] == 'O' && dp[i-1][k][I] > 0){
					dp[i][k][O] = dp[i-1][k][I]+1;
				}
			}

			if(k != 0){
				if(T[k-1] == 'I'){
					dp[i][k][I] = max(dp[i][k][I],dp[i][k-1][O]+1);
				}else if(T[k-1] == 'O' && dp[i][k-1][I] > 0){
					dp[i][k][O] = max(dp[i][k][O],dp[i][k-1][I]+1);
				}
			}
			ans = max(ans,dp[i][k][I]);
		}
	}

	printf("%d\n",ans);

	return 0;
}