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
		len := 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++
				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 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 <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    int dy[] = {-1, 0, 1, 0};
    int dx[] = {0, 1, 0, -1};

    int d;
    cin >> d;

    bool first = true;
    while(--d >= 0){
        if(first)
            first = false;
        else
            cout << endl;

        int n;
        cin >> n;

        int len = n-1;
        int num = 3;
        int y = n-1;
        int x = 0;
        int i = 0;
        vector<string> s(n, string(n, ' '));
        s[y][x] = '#';

        for(;;){
            for(int j=0; j<len; ++j){
                y += dy[i];
                x += dx[i];
                s[y][x] = '#';
            }
            (++i) %= 4;
            if(--num == 0){
                num = 2;
                len -= 2;
            }
            if(n % 2 == 0 && num == 1 && len == 1)
                break;
            if(n % 2 == 1 && len == 0)
                break;
        }

        for(int i=0; i<n; ++i)
            cout << s[i] << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;

constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}
const int dx[] = {0,1,0,-1};
const int dy[] = {-1,0,1,0};
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int d;
  cin >> d;
  rep(i,d){
    if(i != 0) cout << endl;
    int n;
    cin >> n;
    vector<vector<char>> v(n,vector<char>(n,' '));
    rep(i,n){
      v[0][i] = '#';
    }
    if(n % 2){
      int cnt = n-1,dir = 1,x = 0,y = 0;
      v[x][y] = '#';
      while(cnt > 0){
        rep(j,2){
          rep(k,cnt){
            x += dx[dir];
            y += dy[dir];
            if(value(x,y,n,n))
                v[x][y] ='#';
          }
          ++dir;
          dir %=4;
        }
        cnt -= 2;
      }
    }
    else{
      int cnt = n-1,dir = 1,x = 0,y = 0;
      while(cnt > 0){
        rep(j,2){
          rep(k,cnt){
            x += dx[dir];
            y += dy[dir];
            if(value(x,y,n,n))
                v[x][y] ='#';
          }
          ++dir;
          dir %= 4;
        }
        cnt -=2;
      }
      v[n/2][n/2] = ' ' ;
    }
    rep(i,n){
      rep(j,n){
        cout << v[j][i];
      }
      cout << endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <cstring>
using namespace std;
char buf[105][105];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
void solve(int x)
{
	memset(buf,' ',sizeof(buf));
	for(int i=1;i<=x;i++)
	{
		for(int j=1;j<=x;j++)
		{
			buf[i][j]='#';
		}
	}
	int X=x,Y=2;
	int cnt=0;
	for(int p=0;p<=10000;p++)
	{
		if(x%4==1&&X==(x+3)/2&&Y==(x+1)/2)break;
		if(x%4==3&&X==(x+1)/2&&Y==(x+3)/2)break;
		buf[X][Y]=' ';
		X+=dx[cnt];Y+=dy[cnt];
		if(buf[X+dx[cnt]][Y+dy[cnt]]=='#'&&buf[X+dx[cnt]*2][Y+dy[cnt]*2]!='#')
		{
			cnt++;
			cnt%=4;
		}
	}
	for(int i=1;i<=x;i++)
	{
		for(int j=1;j<=x;j++)
		{
			cout << buf[i][j];
		}
		cout << endl;
	}
	return;
}
int main()
{
	int q;
	cin >> q;
	for(int i=0;i<q;i++)
	{
		int k;
		cin >> k;
		solve(k);
	}
	return 0;
}