#include<bits/stdc++.h>
#define PB push_back

using namespace std;
typedef vector<int> VI;

int m, n;
string s, t;

int dp[2001][2001][2];
int ans = 0;

int main()
{
  cin >> m >> n;
  cin >> s >> t;
  
  for(int i = 0; i <= m; i++)
    for(int j = 0; j <= n; j++){
      if(i > 0){
	if(s[i - 1] == 'O' && dp[i - 1][j][1] > 0)
	  dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][1] + 1);
	else if(s[i - 1] == 'I')
	  dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][0] + 1);
      }
      
      if(j > 0){
	if(t[j - 1] == 'O' && dp[i][j - 1][1] > 0)
	  dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][1] + 1);
	else if(t[j - 1] == 'I')
	  dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][0] + 1);
      }
      
      ans = max(ans, dp[i][j][1]);
    }
  
  cout << ans << endl;
  return 0;
}