#include <bits/stdc++.h>
#include <experimental/random>
#include <random>
//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
using namespace std;
using ld = long double;
using ll = long long;
const ll INF = 3e18, MOD = 1e9 + 7;
void solve();
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int q = 1;
//cin >> q;
while (q--) {
solve();
}
}
void solve() {
int n; cin >> n;
string s; cin >> s;
ll c1 = 0, c2 = 0;
ll all = 0;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == 'I') c1++;
if (s[i] == 'O') c2 += c1;
if (s[i] == 'J') {
all += c2;
}
}
ll ans = c2;
ll t1 = 0, t2 = 0;
vector<ll> pref(n);
for (int i = 0; i < n; i++) {
if (s[i] == 'J') t1++;;
if (s[i] == 'O') t2 += t1;
pref[i] = t1;
}
ans = max(ans, t2);
ll f = 0;
for (int i = n - 1; i >= 0; i--) {
if (s[i] == 'I') f++;
ans = max(ans, pref[i] * f);
}
cout << ans + all;
}