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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.*;
import java.io.*;

public class Main {
    public static long gcd(long a, long b){
        if(a<b) return gcd(b,a);
        if(b==0) return a;
        if(a%b==0) return b;
        return gcd(b,a%b);
    }
    public static long lcm(long a, long b){
        return (a/gcd(a,b))*b;
    }
    public static void printMatrix(int N, long[][] matrix){
        for(int i=0;i<N;i++){
        	for(int j=0;j<N;j++){
        	    System.out.print(matrix[i][j]);
        	    if(j>=N-1) System.out.println();
        	    else System.out.print(" ");
        	}
        }
    }
    public static long getMatrix(long[][] matrix, int i, int j){
        try{
        	return matrix[i][j];
        }
        catch(Exception e){
        	return 1;
        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        long[][] matrix = new long[N][N];
        long m = 1;
        long cur = 2;
        for(int i=0;i<N;i+=2) for(int j=0;j<N;j++){
        	if((i+j)%2==0){
        	    matrix[i][j] = cur;
        	    cur++;
        	}
        }
        for(int i=N/2*2-1;i>=0;i-=2) for(int j=N-1;j>=0;j--){
        	if((i+j)%2==0){
        	    matrix[i][j] = cur;
        	    cur++;
        	}
        }
        for(int i=0;i<N;i++) for(int j=0;j<N;j++){
        	if((i+j)%2!=0){
        	    matrix[i][j] = getMatrix(matrix,i-1,j)*getMatrix(matrix,i+1,j)*getMatrix(matrix,i,j-1)*getMatrix(matrix,i,j+1) + m;
        	}
        }
        printMatrix(N,matrix);
    }
}