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<iostream>
#include<string>
#include<iomanip>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
// #define int long long
#define rep(i,n) for(int i = 0; i < n; i++)
#define INF (long long)(1e18)
#define MOD (int)(1e9+7)
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
#define yn(f) (f?"Yes":"No")
#define YN(f) (f?"YES":"NO")
#define pro "はいプロ　世界一○○が上手　○○界のtourist　○○時代の終焉を告げる者　実質○○　○○するために生まれてきた男"
#define Answer_to_the_Ultimate_Question_of_Life_the_Universe_and_Everything 42
#define MAX 110


int dxy[] = {1, -1, 0, 0};
int N, a[MAX], b[MAX], ans[MAX][MAX] = {}, X[MAX], T[MAX*MAX];
set<int> R[MAX];
vector<int> tt;

inline bool is_out_of_range(int z){
	return z <= 0 || N < z ;
}

inline void hoge(int y, int x){
	int temp[MAX] = {};
	set<int> tv;
	int ma = 0, re = 0, count = 0;
	if(y == 1 && x == 1) return;
	for(int i = 0; i < 4; i++){
			int ny = y + dxy[i], nx = x + dxy[3-i];
			if(is_out_of_range(ny)||is_out_of_range(nx)) continue;
			count++;
			if(ans[ny][nx] == 0) continue;
			for(int z : R[ans[ny][nx]]){
			temp[z]++;
			tv.insert(z);
		}
	}
	// sort(tv.begin(), tv.end());
	// tv.erase(unique(tv.begin(),tv.end()),tv.end());
	for(int z : tv){
		if(temp[z] > ma && count == R[z].size()){
			ma = temp[z];
			re = z;
		}
	}
	ans[y][x] = re;
	for(int i = 0; i < 4; i++){
		int ny = y + dxy[i], nx = x + dxy[3-i];
		if(is_out_of_range(ny)||is_out_of_range(nx)) continue;
		if(ans[ny][nx] == 0)continue;
		R[ans[ny][nx]].erase(re);
		R[re].erase(ans[ny][nx]);
	}
}

int huga(){
	queue<pair<int,int>> Q;
	
	Q.push(make_pair(1,1));
	
	
	while(!Q.empty()){
		pair<int,int> P = Q.front();Q.pop();
		int y = P.first, x = P.second; //cout<<y<<" "<<x<<endl;
		for(int i = 0; i < 4; i++){
			int ny = y + dxy[i], nx = x + dxy[3-i];
			if(is_out_of_range(ny)||is_out_of_range(nx)) continue;
			if(ans[ny][nx]) continue;
			Q.push(make_pair(ny,nx));
			hoge(ny,nx);
		}
	}
}

signed main(){

	
	cin>>N;
	
	for(int i = 1; i <= 2*N*N-2*N; i++){
		cin>>a[i]>>b[i];
		tt.push_back(a[i]);
		tt.push_back(b[i]);
	}
	
	sort(tt.begin(),tt.end());
	
	tt.erase(unique(tt.begin(),tt.end()),tt.end());
	
	int i = 1;
	for(int z : tt){//cout<<i<<" "<<z<<endl;
		X[i] = z;
		T[z] = i++;
	}
	
	for(int i = 1; i <= 2*N*N-2*N; i++){
		
		R[T[a[i]]].insert(T[b[i]]);
		R[T[b[i]]].insert(T[a[i]]);
		if(R[T[a[i]]].size() == 2) ans[1][1] = T[a[i]];
		if(R[T[b[i]]].size() == 2) ans[1][1] = T[b[i]];
	}
	
	
	huga();
	
	for(int i = 1; i <= N; i++){
		for(int j = 1; j <= N; j++){
			if(j!=1)cout<<" ";
			cout<<X[ans[i][j]];
		}cout<<endl;
	}
	
	
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;
typedef pair<int,int> P;

P ne(int N,int x,int y){
    if(x==1 && y!=N) return {x,y+1};
    if(y==N && x!=N) return {x+1,y};
    if(x==N && y!=1) return {x,y-1};
    if(y==1) return {x-1,y};
}

int main(){
    int N;
    cin >> N;
    vector<vector<int>> ans(N+1,vector<int>(N+1,0));
    vector<vector<int>> edge(N*N+1),deg(5);
    vector<int> cnt(N*N+1,0),use(N*N+1,0);
    set<int> s[N*N+1];
    int a,b;
    for(int i=0;i<2*N*N-2*N;i++){
        cin >> a >> b;
        edge[a].push_back(b); edge[b].push_back(a);
        cnt[a]++; cnt[b]++;
        s[a].insert(b); s[b].insert(a);   
    }
    for(int i=1;i<=N*N;i++) deg[cnt[i]].push_back(i);
    int x = 1,y = 1,num = deg[2][0];
    ans[1][1] = num;
    use[num] = 1;
    while(true){
        for(auto a:edge[num]){
            if(!use[a] && cnt[a]!=4){
                P p = ne(N,x,y);
                x = p.first; y = p.second;
                ans[x][y] = a;
                use[a] = 1;
                num = a;
                break;
            }
        }
        if(x==2 && y==1) break;
    }
    for(int i=2;i<=N-1;i++) for(int j=2;j<=N-1;j++){
        for(auto a:s[ans[i-1][j]]){
            if(!use[a] && cnt[a]==4 && s[ans[i][j-1]].count(a)==1){
                ans[i][j] = a;
                use[a] = 1;
                break;
            }
        }
    }
    for(int i=1;i<=N;i++) for(int j=1;j<=N;j++) cout << ans[i][j] << (j!=N? " ":"\n");
}