program modulo_matrix
  implicit none
  integer, parameter :: m = 7919, k = 500
  integer :: n, u = 0, i, j
  integer(8) :: a(k,k) = 1_8, p(2*k) = 0_8
  logical :: b(m) = .false.
  b(1) = .true.
  do i = 1, m
    if (b(i)) cycle
    do j = i, m, i
      b(j) = .true.
    end do
    b(i) = .false.
  end do
  do i = 1, m
    if (b(i)) cycle
    u = u+1
    p(u) = i
  end do
  read(*,*) n
  do i = 1, n
    do j = 1, n
      u = i+j
      if (mod(u,2) == 0) a(i,j) = a(i,j)*p(u/2)
      u = i-j+n-1
      if (mod(u,2) /= mod(n,2)) a(i,j) = a(i,j)*p(2*n-(u+1)/2)
    end do
  end do
  do i = 1, n
    do j = 1, n
      if (a(i,j) /= 1_8) cycle
      if (i /= 1) a(i,j) = lcm(a(i,j),a(i-1,j))
      if (i /= n) a(i,j) = lcm(a(i,j),a(i+1,j))
      if (j /= 1) a(i,j) = lcm(a(i,j),a(i,j-1))
      if (j /= n) a(i,j) = lcm(a(i,j),a(i,j+1))
      a(i,j) = a(i,j)+1_8
    end do
  end do
  do i = 1, n
    write(*,'(i0)',advance='no') a(i,1)
    do j = 2, n
      write(*,'(x,i0)',advance='no') a(i,j)
    end do
    write(*,*)
  end do
contains
  integer(8) function lcm(a,b)
    integer(8), intent(in) :: a, b
    lcm = abs(a)/gcd(a,b)*abs(b)
  end
  integer(8) function gcd(a,b)
    integer(8), intent(in) :: a, b
    integer(8) :: x, y
    x = max(abs(a),abs(b))
    y = min(abs(a),abs(b))
    do while (y > 0_8)
      gcd = y
      y = mod(x,y)
      x = gcd
    end do
  end
end program modulo_matrix