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 REP(i,st,ed) for(register int i=st,i##end=ed;i<=i##end;++i)
#define DREP(i,st,ed) for(register int i=st,i##end=ed;i>=i##end;--i)
typedef long long ll;
template<typename T>inline bool chkmin(T &x,T y){return (y<x)?(x=y,1):0;}
template<typename T>inline bool chkmax(T &x,T y){return (y>x)?(x=y,1):0;}
inline int read(){
    int x;
    char c;
    int f=1;
    while((c=getchar())!='-' && (c>'9' || c<'0'));
    if(c=='-') f=-1,c=getchar();
    x=c^'0';
    while((c=getchar())>='0' && c<='9') x=(x<<1)+(x<<3)+(c^'0');
    return x*f;
}
inline ll readll(){
    ll x;
    char c;
    int f=1;
    while((c=getchar())!='-' && (c>'9' || c<'0'));
    if(c=='-') f=-1,c=getchar();
    x=c^'0';
    while((c=getchar())>='0' && c<='9') x=(x<<1ll)+(x<<3ll)+(c^'0');
    return x*f;
}
const int maxn=1e7+10;
ll a[505][505];
int prime[maxn],isprime[maxn],tmp;
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
void init(int n){
	REP(i,2,n){
		if(!isprime[i]) prime[++tmp]=i;
		REP(j,1,tmp){
			if((ll)i*prime[j]>n) break;
			isprime[i*prime[j]]=1;
			if(i%prime[j]==0) break;
		}
	}
}
ll lcm(ll x,ll y){
	return (x/__gcd(x,y))*y;
}
int main(){
	int n=read();
	if(n==2){
		printf("4 7\n23 10\n");
		return 0;
	}
	init(1e7);
	int l=1,r=n;
	for(int i=2;i<=2*n;i+=2){
		int u;
		if(i%4) u=prime[r--];
		else u=prime[l++];
		REP(j,1,n){
			int k=i-j;
			if(k>=1 && k<=n) a[j][k]=u;
		}
	}
	l=n+1,r=n*2;
	for(int i=2;i<=2*n;i+=2){
		int u;
		if(i%4) u=prime[r--];
		else u=prime[l++];
		REP(j,1,n){
			int k=n+1+j-i;
			if(k>=1 && k<=n) a[j][k]*=u;
		}
	}
	REP(i,1,n) REP(j,1,n) if((i+j)&1){
		a[i][j]=1;
		REP(k,0,3){
			int u=i+dir[k][0],v=j+dir[k][1];
			if(u>=1 && u<=n && v>=1 && v<=n) a[i][j]=lcm(a[i][j],a[u][v]);
		}
		a[i][j]++;
	}
	REP(i,1,n) REP(j,1,n) printf("%lld%c",a[i][j],j==jend?'\n':' ');
    return 0;
}