Go: package main

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

func main() {
	io := NewIo()
	defer io.Flush()
	N := io.NextInt()
	nodes := make([]Node, N*N+1)
	for i := 0; i < 2*N*N-2*N; i++ {
		a, b := io.NextInt(), io.NextInt()
		nodes[a].adj = append(nodes[a].adj, b)
		nodes[b].adj = append(nodes[b].adj, a)
	}
	ninja(nodes, N, io)
}

type Node struct {
	adj []int
}

func ninja(nodes []Node, N int, io *Io) {
	used := make([]bool, N*N+1)
	m := make([][]int, N)
	fst := 0
	for i := 0; i < N; i++ {
		m[i] = make([]int, N)
		for j := 0; j < N; j++ {
			if len(nodes[i*N+j].adj) == 2 {
				fst = i*N + j
			}
		}
	}
	m[0][0] = fst
	used[fst] = true
	for j := 1; j < N; j++ {
		u := 0
		deg := 3
		if j == N-1 {
			deg = 2
		}
		for _, v := range nodes[m[0][j-1]].adj {
			if !used[v] && len(nodes[v].adj) == deg {
				u = v
			}
		}
		m[0][j] = u
		used[u] = true
	}
	for i := 1; i < N; i++ {
		for j := 0; j < N; j++ {
			u := 0
			for _, v := range nodes[m[i-1][j]].adj {
				if !used[v] {
					u = v
				}
			}
			m[i][j] = u
			used[u] = true
		}
	}
	for _, a := range m {
		io.PrintIntSlice(a)
	}
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n;
vector<int> G[10001];
int res[101][101];
bool used[10001];
vector<int> vs;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

void dfs(int v){
	used[v]=true;
	vs.push_back(v);
	for(int i=0;i<G[v].size();i++){
		int nv=G[v][i];
		if(!used[nv] && G[nv].size()<=3){
			dfs(nv);
		}
	}
}

int main(void){
	scanf("%d",&n);
	for(int i=0;i<2*n*n-2*n;i++){
		int u,v;
		scanf("%d%d",&u,&v);
		u--;
		v--;
		G[u].push_back(v);
		G[v].push_back(u);
	}
	memset(used,false,sizeof(used));
	int top;
	for(int i=0;i<n*n;i++){
		if(!used[i] && G[i].size()==2){
			dfs(i);
		}
	}
	int x=0,y=0,dir=0;
	queue<int> que[101];
	for(int i=0;i<vs.size();i++){
		res[y][x]=vs[i];
		if(y==0 && x>0 && x<n-1){
			que[x].push(vs[i]);
		}
		x+=dx[dir];
		y+=dy[dir];
		if(x==n-1 && y==0)dir++;
		if(x==n-1 && y==n-1)dir++;
		if(x==0 && y==n-1)dir++;
	}
	for(int i=0;i<n-1;i++){
		for(int j=1;j<n-1;j++){
			int v=que[j].front();
			que[j].pop();
			res[i][j]=v;
			for(int k=0;k<G[v].size();k++){
				int nv=G[v][k];
				if(!used[nv]){
					used[nv]=true;
					que[j].push(nv);
				}
			}
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			printf("%d%c",res[i][j]+1,j+1==n?'\n':' ');
		}
	}
	return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
	return (ull)rng() % B;
}

vector<int> g[10010];

int res[101][101];

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    for(int i=0;i<2*n*n-2*n;i++){
        int a,b; cin >> a >> b;
        a--; b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for(int i=0;i<2*n*n-2*n;i++){
        if(g[i].size()==2){
            res[0][0]=i;
            res[0][1]=g[i][0];
            res[1][0]=g[i][1];
            break;
        }
    }
    for(int i=2;i<n;i++){
        int pp=res[0][i-2];
        int p=res[0][i-1];
        for(int t:g[p]){
            if(g[t].size()<=3&&t!=pp){
                res[0][i]=t;
            }
        }
    }
    for(int i=2;i<n;i++){
        int pp=res[i-2][0];
        int p=res[i-1][0];
        for(int t:g[p]){
            if(g[t].size()<=3&&pp!=t){
                res[i][0]=t;
            }
        }
    }
    for(int i=1;i<n;i++){
        for(int j=1;j<n;j++){
            int r=res[i-1][j-1];
            for(int s:g[res[i-1][j]]){
                for(int t:g[res[i][j-1]]){
                    if(s==t&&s!=r){
                        res[i][j]=s;
                    }
                }
            }
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(j)printf(" ");
            printf("%d",res[i][j]+1);
        }
        printf("\n");
    }
}