#include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)

using namespace std;

#define I 1
#define O 0

#define MAX 2010


int memo[MAX][MAX][2];
int N,M;
string S,T;

int dfs(int n,int m,int type) {
  if( n == 0 && m == 0 ) {
    if( type == O ) return -2;
    return 1;
  }
  int &ret = memo[n][m][type];
  if( ret != -1 ) return ret;
  if( type == O ) ret = 0;
  else            ret = 1;
  if( n ) {
    int new_type = ( S[N-n] == 'I' ) ? I : O;
    if( type != new_type ) {
      int v = dfs(n-1,m,new_type);
      if( v != -2 ) ret = max(ret,v+1);
    }
  }
  if( m ) {
    int new_type = ( T[M-m] == 'I' ) ? I : O;
    if( type != new_type ) {
      int v = dfs(n,m-1,new_type);
      if( v != -2 ) ret = max(ret,v+1);
    }
  }
  return ret;
}

void compute() {
  memset(memo,-1,sizeof memo);
  int maxi = 0;
  rep(n,N+1) rep(m,M+1) {
    if( n && S[N-n] == 'I' ) {
      maxi = max(maxi,dfs(n-1,m,I));
    }
    if( m && T[M-m] == 'I' ) {
      maxi = max(maxi,dfs(n,m-1,I));
    }
  }
  cout << maxi << endl;
}

int main() {
  cin >> N >> M >> S >> T;
  compute();
  return 0;
}