class Map
  def initialize(x,y,number,move)
    @x = x
    @y = y
    @move = move
    @number = number
  end
  attr_accessor :x,:y,:move,:number
  def +(map)
    x = self.x + map.x
    y = self.y + map.y
    m = self.move
    n = self.number + 1
    tmp = Map.new(x,y,n,m)
  end
end
@b = []
@b << Map.new(1,0, 1,0) #sita
@b << Map.new(0,1, 1,1) #migi
@b << Map.new(1,-1,1,2) #hidarisita
@b << Map.new(-1,1,1,3) #migiue

def work(map,m)
  n=m-1
  if map.move==3 && map.x!=0 && map.y!=n then
    return map+@b[3]
  elsif map.move==2 && map.y!=0 && map.x!=n then
    return map+@b[2]
  elsif (map.move==3&&map.x==0)||(map.move==2&&map.x==n) then
    if map.move==3 then
      move = 2
    else
      move = 3
    end
    temp = map + @b[1]
    temp.move = move
    if map.y==n then
      temp.move = 2
      temp.x = 1
      temp.y = n
      return temp
    else
      return temp
    end
  else
    if map.move==3 then
      move = 2
    else
      move = 3
    end
    temp = map + @b[0]
    temp.move = move
    return temp
  end
end
$m = Map.new(0,0,1,2)
$n = 3
def w(m,n)
  return work( m, n )
end
$COU = 0
def printmap(n)
  $COU = $COU + 1
  puts "Case %d:" % $COU
  c=[$m]
  i=1
  while i<n*n
    c << w(c[i-1],n)
    i = i+1
  end
  i=0
  while i < n
    for mapp in c do
      if mapp.y==i then
        print "%3d" % mapp.number
      end
    end
    puts
  i=i+1
  end
end
while true
  a = gets.to_i
  if a==0 then
    break
  end
  printmap(a)
end