n = int(input()) if n == 0: exit() grid = [[0 for _ in range(n)] for _ in range(n)] current_row, current_col = 0, 0 grid[current_row][current_col] = 1 directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] # right, down, left, up current_dir = 0 # start with right for num in range(2, n * n + 1): dr, dc = directions[current_dir] next_row = current_row + dr next_col = current_col + dc # Check if the next cell is valid (within bounds and not filled) if 0 <= next_row < n and 0 <= next_col < n and grid[next_row][next_col] == 0: current_row, current_col = next_row, next_col else: # Change direction clockwise current_dir = (current_dir + 1) % 4 dr, dc = directions[current_dir] current_row += dr current_col += dc grid[current_row][current_col] = num for row in grid: formatted = [f"{num:03d}" for num in row] print(" ".join(formatted))