Problem:
Score : 1100 points

Problem Statement

You are given an integer N.

Construct any one N-by-N matrix a that satisfies the conditions below. It can be proved that a solution always exists under the constraints of this problem.

1 \leq a_{i,j} \leq 10^{15}

a_{i,j} are pairwise distinct integers.

There exists a positive integer m such that the following holds: Let x and y be two elements of the matrix that are vertically or horizontally adjacent. Then, {\rm max}(x,y) {\rm mod} {\rm min}(x,y) is always m.

Constraints

2 \leq N \leq 500

Input

Input is given from Standard Input in the following format:

N

Output

Print your solution in the following format:

a_{1,1} ... a_{1,N}
:
a_{N,1} ... a_{N,N}

Sample Input 1

2

Sample Output 1

4 7
23 10

For any two elements x and y that are vertically or horizontally adjacent, {\rm max}(x,y) {\rm mod} {\rm min}(x,y) is always 3.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
#define maxn 520
#define maxm 20020
#define rep(i,l,r) for(register int i = l ; i <= r ; i++)
#define repd(i,r,l) for(register int i = r ; i >= l ; i--)
#define rvc(i,S) for(register int i = 0 ; i < (int)S.size() ; i++)
#define rvcd(i,S) for(register int i = ((int)S.size()) - 1 ; i >= 0 ; i--)
#define fore(i,x)for (register int i = head[x] ; i ; i = e[i].next)
#define pb push_back
#define prev prev_
#define stack stack_
#define mp make_pair
#define fi first
#define se second
#define inf 0x3f3f3f3f
typedef long long ll;
typedef pair<int,int> pr;

int prime[maxm],cnt,tag[maxm];
int n,tag1[maxn][maxn],tag2[maxn][maxn],tot;
ll a[maxn][maxn];
map <ll,int> num;
vector <pr> vec;
ll tmp = 1e15;

void init(){
	rep(i,2,10000){
		if ( !tag[i] ) prime[++cnt] = i;
		rep(j,1,cnt){
			if ( prime[j] * i > 10000 ) break;
			tag[prime[j] * i] = 1;
			if ( i % prime[j] == 0 ) break;
		}
	}
}
ll gcd(ll a,ll b){
	if ( !b ) return a;
	return gcd(b,a % b);
}
inline ll lcm(ll a,ll b){
	ll d = gcd(a,b);
	return a / d * b;
}
int main(){
	init();
	scanf("%d",&n);
		
	if ( n == 2 ){
		printf("4 7\n23 10\n");
		return 0;
	}
	rep(i,1,n){
		rep(j,1,n){
			if ( ((i + j) & 1 ) ){
				if ( i > 1 && j > 1 && tag1[i - 1][j - 1] ) tag1[i][j] = tag1[i - 1][j - 1];
				if ( i < n && j < n && tag1[i + 1][j + 1] ) tag1[i][j] = tag1[i + 1][j + 1];
				if ( !tag1[i][j] ) tag1[i][j] = ++tot; 
				if ( i > 1 && j < n && tag2[i - 1][j + 1] ) tag2[i][j] = tag2[i - 1][j + 1];
				if ( i < n && j > 1 && tag2[i + 1][j - 1] ) tag2[i][j] = tag2[i + 1][j - 1];
				if ( !tag2[i][j] ) tag2[i][j] = ++tot;
			}
		}
	}
	rep(i,1,n){
		rep(j,1,n){
			if ( ((i + j) & 1) ){
				a[i][j] = prime[tag1[i][j]] * prime[tag2[i][j]];
			}
		}
	}
	rep(i,1,n){
		rep(j,1,n){
			if ( !a[i][j] ){
				ll cur = 1;
				if ( i > 1 ) cur = lcm(cur,a[i - 1][j]);
				if ( i < n ) cur = lcm(cur,a[i + 1][j]);
				if ( j > 1 ) cur = lcm(cur,a[i][j - 1]);
				if ( j < n ) cur = lcm(cur,a[i][j + 1]);
				a[i][j] = cur + 1;
			}
		}
	}
	rep(i,1,n){
		rep(j,1,n){
			cout<<a[i][j]<<" ";

		}
		cout<<endl;
	}

	//rvc(i,vec) cerr<<vec[i].fi<<" "<<vec[i].se<<endl;
}