using System; class A { static int N; static int[,] Q; static int Dir = 0; static int[] Pos = new int[2]; static void Main() { N = int.Parse(Console.ReadLine()); Q = new int[N, N]; for(int i = 1; i <= N * N; i++) { Q[Pos[0], Pos[1]] = i; Next(); } for(int i = 0; i < N; i++) { for(int j = 0; j < N - 1; j++) { Console.Write(string.Format("{0:D3}", Q[j,i])); Console.Write(" "); } Console.Write(string.Format("{0:D3}", Q[N-1,i])); Console.WriteLine(); } } static void Next() { switch (Dir) { case 0: if (Pos[0] < N-1) { if (Q[Pos[0] + 1, Pos[1]]!=0) { Dir = 1; Pos[1]++; } else { Pos[0]++; } } else { Dir = 1; Pos[1]++; } break; case 1: if (Pos[1] < N-1) { if (Q[Pos[0], Pos[1] + 1] != 0) { Dir = 2; Pos[0]--; } else { Pos[1]++; } } else { Dir = 2; Pos[0]--; } break; case 2: if (Pos[0] > 0) { if (Q[Pos[0] - 1, Pos[1]] != 0) { Dir = 3; Pos[1]--; } else { Pos[0]--; } } else { Dir = 3; Pos[1]--; } break; case 3: if (Q[Pos[0], Pos[1] - 1] != 0) { Dir = 0; Pos[0]++; } else { Pos[1]--; } break; } } }