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, 100)
	}
	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.Println("##")
			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()
		}
	}
}

C++: #include<stdio.h>
char out[100][101];
int dx[]={0,1,0,-1};
int dy[]={-1,0,1,0};
int main(){
	int a;
	scanf("%d",&a);
	while(a--){
		int b;
		scanf("%d",&b);
		for(int i=0;i<b;i++){
			for(int j=0;j<b;j++){
				out[i][j]=' ';
			}
			out[i][b]=0;
		}
		if(b==1){
			printf("#\n");
			if(a)printf("\n");
			continue;
		}
		if(b==2){
			printf("##\n# \n");
			if(a)printf("\n");
			continue;
		}
		int row=b-1;
		int col=0;
		int dir=0;
		int len=1;
		out[row][col]='#';
		while(1){
			int nrow=row+dy[dir%4];
			int 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++;
				len=1;
			}else if(out[nrow+dy[dir%4]][ncol+dx[dir%4]]=='#'){
				if(len<3)break;
				dir++;
				len=1;
			}else{
				row=nrow;
				col=ncol;
				out[row][col]='#';
				len++;
			}
		}
		for(int i=0;i<b;i++)printf("%s\n",out[i]);
		if(a)printf("\n");
	}
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF=1e9;
const int MOD=100000;
int main(){
	int d,n;
	cin>>d;
	REP(times,d){
		cin>>n;
		char fld[n][n],a,b;
		REP(i,(n+1)/2){
			if(i%2==0){a='#';b=' ';}
			else{a=' ';b='#';}
			REP(j,(n-i*2)){
				fld[i+j][i]=a;
				fld[i][i+j]=a;
				fld[n-1-i][i+j]=a;
				fld[i+j][n-1-i]=a;
			}
			fld[i+1][n-1-i]=b;
		}
		if(n%4==0)fld[n/2][n/2]=' ';
		REP(i,n){
			REP(j,n)cout<<fld[j][i];
			cout<<endl;
		}
		cout<<endl;
	}
}