Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"fmt"
	)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	out := make([][]string, 100)
	for i := range out {
		out[i] = make([]string, 101)
	}
	dx := []int{0, 1, 0, -1}
	dy := []int{-1, 0, 1, 0}
	scanner.Scan()
	a, _ := strconv.Atoi(scanner.Text())
	for a > 0 {
		a--
		scanner.Scan()
		b, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < b; i++ {
			for j := 0; j < b; j++ {
				out[i][j] = " "
			}
			out[i][b] = "0"
		}
		if b == 1 {
			fmt.Println("#")
			if a != 0 {
				fmt.Println()
			}
			continue
		}
		if b == 2 {
			fmt.Printf("##\n# \n")
			if a != 0 {
				fmt.Println()
				continue
			}
		}
		row := b -1
		col := 0
		dir := 0
		l := 0
		out[row][col] = "#"
		for {
			nrow := row+dy[dir%4]
			ncol := col+dx[dir%4]
			if dir%4 == 0 && nrow == 0 || dir%4==3&&ncol==0 || dir%4==2&&nrow==b-1 || dir%4==1&&ncol==b-1 {
				row=nrow
				col=ncol
				out[row][col]="#"
				dir++
				l = 1
			} else if out[nrow+dy[dir%4]][ncol+dx[dir%4]]=="#" {
				if l < 3 {
					break
				}
				dir++
				l = 1
			} else {
				row=nrow
				col=ncol
				out[row][col]="#"
				l++
			}
		}
		for i := 0; i < b; i++ {
			for j := 0; j < b; j++ {
				fmt.Printf("%s", out[i][j])
			}
			fmt.Println()
		}
		if a != 0 {
			fmt.Println()
		}
	}
}

Python: class Vector:

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def move(self, offset):
        self.x += offset[0]
        self.y += offset[1]

    def move_offset(self, offset, multiple=1):
        x = self.x + offset[0] * multiple
        y = self.y + offset[1] * multiple
        return Vector(x, y)


class Pattern:
    NOTHING = " "
    EXIST = "#"
    SENTINEL = "?"

    MOVE = [
        [[-1, -1], [-1, +0], [-1, +1]],
        [[-1, +1], [-0, +1], [+1, +1]],
        [[+1, +1], [+1, +0], [+1, -1]],
        [[+1, -1], [+0, -1], [-1, -1]],
    ]

    @classmethod
    def create_area(cls, size):
        area = [[cls.SENTINEL] * 2 + [cls.NOTHING] * size + [cls.SENTINEL] * 2 for _ in range(size)]
        tmp = [[cls.SENTINEL] * size + [cls.SENTINEL] * 2 * 2]
        area = tmp * 2 + area + tmp * 2
        return area

    @classmethod
    def even_spiral_pattern(cls, area, point):
        move_index = 0
        area[point.x][point.y] = cls.EXIST

        while True:

            left, center, right = cls.MOVE[move_index]
            end1, end2 = point.move_offset(left), point.move_offset(right)
            offset, offset2 = point.move_offset(center), point.move_offset(center, 2)

            if area[end1.x][end1.y] == cls.EXIST or area[end2.x][end2.y] == cls.EXIST:
                return area
            elif area[offset.x][offset.y] == cls.NOTHING and area[offset2.x][offset2.y] != cls.EXIST:
                point.move(center)
                area[point.x][point.y] = cls.EXIST
            else:
                move_index += 1
                move_index %= 4

    @classmethod
    def odd_spiral_pattern(cls, area, point):
        move_index = 0
        is_end = False
        area[point.x][point.y] = cls.EXIST

        while True:

            left, center, right = cls.MOVE[move_index]
            offset, offset2 = point.move_offset(center), point.move_offset(center, 2)

            if area[offset.x][offset.y] == cls.NOTHING and area[offset2.x][offset2.y] != cls.EXIST:
                point.move(center)
                area[point.x][point.y] = cls.EXIST
                is_end = False
            else:
                if is_end:
                    return area
                else:
                    is_end = True

                move_index += 1
                move_index %= 4

    @classmethod
    def formater(cls, area):
        output = ["".join(item[2:-2]) for item in area[2:-2]]
        output = "\n".join(output)
        return output

    @classmethod
    def solve(cls, size):
        area = cls.create_area(size)
        point = Vector(size - 1 + 2, 2)

        if size % 2 == 0:
            result = cls.even_spiral_pattern(area, point)
        else:
            result = cls.odd_spiral_pattern(area, point)

        return cls.formater(result)


sizes = [int(input()) for _ in range(int(input()))]
results = [Pattern.solve(num) for num in sizes]
print("\n\n".join(results))

Python: # -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0141

"""
import sys
from sys import stdin
input = stdin.readline
from itertools import cycle


def guruguru(n):
    dirs = cycle([(0, -1), (1, 0), (0, 1), (-1, 0)]) #  x, y???????????? (???????????????????????????)

    M = [[' '] * n for _ in range(n)] #  ??????????????¨?????????????????§?????????
    cx = 0                      #  ??????????????°???
    cy = n - 1
    line = 0                    #  ???????????§??????????????????n??¨???????????°??????????????¨??????

    while line < n:
        line += 1
        dx, dy = dirs.__next__() #  ???????????????????????????????????????????????????
        while True:
            M[cy][cx] = '#'
            nx = cx + dx        #  ?¬???????????????¨???????????????
            ny = cy + dy
            nnx = nx + dx       #  ?¬?????¬???????????????¨???????????????
            nny = ny + dy

            if nx < 0 or nx >= n: #  ??????????????????????????????????????§???????????¢?????????
                break
            if ny < 0 or ny >= n:
                break
            if nny < 0 or nny >= n or nnx < 0 or nnx >= n:
                pass
            else:
                if M[nny][nnx] != ' ': #  ??????????????????????????????????????¢??????????????????????????£??????????????´??????????????¢?????????
                    break
            cx = nx
            cy = ny

    # ?????????????????????????????????
    for l in M:
        print(''.join(l))


def main(args):
    n = int(input())
    for i in range(n):
        size = int(input())
        guruguru(size)
        if i != (n - 1):        #  ????????\?????????????????????????????????????????\??????
            print()


if __name__ == '__main__':
    main(sys.argv[1:])

Python: def p(t):
    for r in t:
        for c in r:
            print(c,end="")
        print("")
        
a = int(input())
for j in range(a):
    n = int(input())
    t = [ [ " " for _ in range(n) ] for _ in range(n)]
    d = 1 # 0:up, 1:right, 2:down, 3:left

    for i in range(n):
        t[i][0] = "#"

    x = 0
    y = 0
    for m in range(n-1):
        l = n - 1 - (m//2)*2
        for i in range(l):
            if d == 0:
                y -= 1
            elif d == 1:
                x += 1
            elif d == 2:
                y += 1
            elif d == 3:
                x -= 1
                
            t[y][x] = "#"

        d = (d + 1) % 4
    p(t)
    if j != a-1:
        print("")

Python: # -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0141

"""
import sys
from sys import stdin
input = stdin.readline
from itertools import cycle


def guruguru(n):
    if n < 5:
        return

    line = 0
    dirs = cycle([(0, -1), (1, 0), (0, 1), (-1, 0)])

    M = [[' '] * n for _ in range(n)]
    cx = 0
    cy = n - 1

    while line < n:
        line += 1
        dx, dy = dirs.__next__()
        while True:
            M[cy][cx] = '#'
            nx = cx + dx
            ny = cy + dy
            nnx = nx + dx
            nny = ny + dy

            if nx < 0 or nx >= n:
                break
            if ny < 0 or ny >= n:
                break
            if nny < 0 or nny >= n or nnx < 0 or nnx >= n:
                pass
            else:
                if M[nny][nnx] != ' ':
                    break
            cx = nx
            cy = ny

    for l in M:
        print(''.join(map(str, l)))


def main(args):
    for line in sys.stdin:
        n = int(line)
        guruguru(n)


if __name__ == '__main__':
    main(sys.argv[1:])