#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
string S;
int X[1000005][3];
int z=0;
int y=0;
int main()
{
	cin >> S;
	if(S[0]=='J'){
		X[z][0]++;
		y=0;
	}
	if(S[0]=='O'){
		X[z][1]++;
		y=1;
	}
	if(S[0]=='I'){
		X[z][2]++;
		y=2;
	}
	for(int i=1;i<S.length();i++){
		if(S[i-1]==S[i])X[z][y]++;
		else{
			z++;
			if(S[i]=='J')y=0;
			if(S[i]=='O')y=1;
			if(S[i]=='I')y=2;
			X[z][y]++;
		}
	}
	int ans=0;
	for(int i=0;i<=z;i++){
		if(X[i][0]<X[i+1][1])continue;
		if(X[i+1][1]>X[i+2][2])continue;
		ans=max(ans,X[i+1][1]);
	}
	printf("%d\n",ans);
	return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
typedef  long long ll;
ll gcd(ll f , ll s){if(s==0)return f;else return gcd(s,f%s);}
int const N = 1000006;
ll const M = 998244353;
long double const ep = .000000000000000001;
int p[N];
queue<int>q;
ll Farray[1032][1032] , Sarray[1032][1023] , arr[1002][1003];
int main() {
	
	int n;cin >> n;
	if(n==2){
		cout << 4 << " " << 7 << endl << 23 << " " << 10 ;
		return 0;
	}
	for(int i = 2 ; i <= 1000 ; i++){
		if(!p[i]){
			for(int x = i + i; x <= 100000 ; x+=i)p[x] = 1;
		}
	}	
	for(int i = 2 ; i <= 100000 ; i++)if(!p[i])q.push(i);
	pair<int,int>start = mp(0,0);
	for(pair<int,int> i = start ; i.first < n ; i.first += 2){
		int currP = q.front();
		q.pop();
		for(pair<int,int> x = i ; x.first < n && x.second < n ; x.first++ , x.second++){
			Farray[x.first][x.second] = currP;
		}
	}
	for(pair<int,int> i = mp(0,2) ; i.second < n ; i.second += 2){
		int currP = q.front();
		q.pop();
		for(pair<int,int> x = i ; x.first < n && x.second < n ; x.first++ , x.second++){
			Farray[x.first][x.second] = currP;
		}
	}
	
	for(pair<int,int> i = start ; i.second < n ; i.second += 2){
		int currP = q.front();
		q.pop();
		for(pair<int,int> x = i ; x.first < n && x.second >= 0 ; x.first++ , x.second--){
			Sarray[x.first][x.second] = currP;
		}
	}

	for(pair<int,int> i = mp(1+(n%2),n-1) ; i.first < n ; i.first += 2){
		int currP = q.front();
		q.pop();
		for(pair<int,int> x = i ; x.first < n && x.second >= 0 ; x.first++ , x.second--){
			Sarray[x.first][x.second] = currP;
		}
	}


	for(int i = 0 ; i < n ; i++)
		for(int x = 0 ; x < n ; x++)
			arr[i][x] = Farray[i][x] * Sarray[i][x];
	for(int i=0 ; i< n ; i++){
		for(int j=0 ; j< n ; j++){
			if((i + j)%2== 1){
				set<ll>s;
				if(i != 0){
					s.insert(Farray[i-1][j]);
					s.insert(Sarray[i-1][j]);
				}
				if(i != n-1){
					s.insert(Farray[i+1][j]);
					s.insert(Sarray[i+1][j]);
				}
				if(j != 0){
					s.insert(Farray[i][j-1]);
					s.insert(Sarray[i][j-1]);
				}
				if(j != n-1){
					s.insert(Farray[i][j+1]);
					s.insert(Sarray[i][j+1]);
				}
				arr[i][j] = 1;
				set<ll>::iterator it = s.begin();
				for(;it!=s.end() ; it++)arr[i][j]*= *(it);
				arr[i][j]++;
			}
		}
	}
	for(int i=0 ; i< n ; i++){
		for(int j=0 ; j< n ; j++){
			cout << arr[i][j] << " ";
		}
		cout << endl;
	}
	return 0;	
}