#include<cstdio>
#include<algorithm>
using namespace std;

int main(){
  char s[2001],t[2001];
  int m,n;
  static int dp[2][2001][2001];
  scanf("%d %d %s %s",&m,&n,s,t);
  int ans = 0;
  for(int i=0;i<=m;i++){
    for(int j=0;j<=n;j++){
      dp[0][i][j] = 0;
      dp[1][i][j] = 0;
      if(i!=0){
        if(s[i-1]=='I') dp[0][i][j] = max(dp[0][i][j],dp[1][i-1][j] + 1);
        else if(dp[0][i-1][j]>0&&s[i-1]=='O') dp[1][i][j] = max(dp[1][i][j],dp[0][i-1][j] + 1);
      }
      if(j!=0){
        if(t[j-1]=='I') dp[0][i][j] = max(dp[0][i][j],dp[1][i][j-1] + 1);
        else if(dp[0][i][j-1]>0&&t[j-1]=='O') dp[1][i][j] = max(dp[1][i][j],dp[0][i][j-1] + 1);
      }
      ans = max(ans,dp[0][i][j]);
    }
  }
  printf("%d\n",ans);
  return 0;
}