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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


enum Type{
	IN_KADO,
	IN_HEN,
	IN_NAIBU,
};

struct Info{
	void set(Type arg_type){
		type = arg_type;
	}
	Type type;
};


int main(){

	int N;
	scanf("%d",&N);

	int* in_num = new int[N*N];
	bool* used = new bool[N*N];
	Info* loc = (Info*)malloc(sizeof(Info)*(N*N));

	for(int i = 0; i < N*N; i++){
		in_num[i] = 0;
		used[i] = false;
	}

	vector<int> ADJ[N*N];

	int a,b,limit = 2*N*N-2*N;
	for(int loop = 0; loop < limit; loop++){
		scanf("%d %d",&a,&b);
		a--;
		b--;
		in_num[a]++;
		in_num[b]++;
		ADJ[a].push_back(b);
		ADJ[b].push_back(a);
	}

	vector<int> KADO;
	for(int i = 0; i < N*N; i++){
		if(in_num[i] == 2){
			KADO.push_back(i);
			loc[i].set(IN_KADO);
		}else if(in_num[i] == 3){
			loc[i].set(IN_HEN);
		}else{
			loc[i].set(IN_NAIBU);
		}
	}

	int** ans_table = new int*[N];
	for(int row = 0; row < N; row++){
		ans_table[row] = new int[N];
	}

	ans_table[0][0] = KADO[0];
	used[KADO[0]] = true;

	int tmp_num;

	for(int row = 1; row <= N-2; row++){
		for(int i = 0; i < ADJ[ans_table[row-1][0]].size(); i++){
			tmp_num = ADJ[ans_table[row-1][0]][i];
			if(loc[tmp_num].type == IN_HEN && used[tmp_num] == false){
				ans_table[row][0] = tmp_num;
				used[tmp_num] = true;
				break;
			}
		}
	}

	for(int i = 0; i < ADJ[ans_table[N-2][0]].size(); i++){
		tmp_num = ADJ[ans_table[N-2][0]][i];
		if(loc[tmp_num].type == IN_KADO && used[tmp_num] == false){
			ans_table[N-1][0] = tmp_num;
			used[tmp_num] = true;
			break;
		}
	}

	bool FLG;

	for(int col = 1; col <= N-2; col++){

		for(int i = 0; i < ADJ[ans_table[0][col-1]].size(); i++){
			tmp_num = ADJ[ans_table[0][col-1]][i];
			if(loc[tmp_num].type == IN_HEN && used[tmp_num] == false){
				ans_table[0][col] = tmp_num;
				used[tmp_num] = true;
				break;
			}
		}

		for(int row = 1; row <= N-2; row++){

			for(int i = 0; i < ADJ[ans_table[row-1][col]].size(); i++){
				tmp_num = ADJ[ans_table[row-1][col]][i];
				if(loc[tmp_num].type != IN_NAIBU || used[tmp_num] == true)continue;

				FLG = false;
				for(int k = 0; k < ADJ[ans_table[row][col-1]].size(); k++){
					if(tmp_num == ADJ[ans_table[row][col-1]][k]){
						FLG = true;
						break;
					}
				}

				if(FLG){
					ans_table[row][col] = tmp_num;
					used[tmp_num] = true;
					break;
				}
			}
		}

		for(int i = 0; i < ADJ[ans_table[N-1][col-1]].size(); i++){
			tmp_num = ADJ[ans_table[N-1][col-1]][i];
			if(loc[tmp_num].type == IN_HEN && used[tmp_num] == false){
				ans_table[N-1][col] = tmp_num;
				used[tmp_num] = true;
				break;
			}
		}
	}

	for(int i = 0; i < ADJ[ans_table[0][N-2]].size(); i++){
		tmp_num = ADJ[ans_table[0][N-2]][i];
		if(loc[tmp_num].type == IN_KADO && used[tmp_num] == false){
			ans_table[0][N-1] = tmp_num;
			used[tmp_num] = true;
			break;
		}
	}

	for(int row = 1; row <= N-2; row++){
		for(int i = 0; i < ADJ[ans_table[row-1][N-1]].size(); i++){
			tmp_num = ADJ[ans_table[row-1][N-1]][i];
			if(loc[tmp_num].type == IN_HEN && used[tmp_num] == false){
				ans_table[row][N-1] = tmp_num;
				used[tmp_num] = true;
				break;
			}
		}
	}

	for(int i = 0; i < ADJ[ans_table[N-1][N-2]].size(); i++){
		tmp_num = ADJ[ans_table[N-1][N-2]][i];
		if(loc[tmp_num].type == IN_KADO && used[tmp_num] == false){
			ans_table[N-1][N-1] = tmp_num;
			break;
		}
	}


	for(int row = 0; row <= N-1; row++){
		printf("%d",ans_table[row][0]+1);
		for(int col = 1; col <= N-1; col++){
			printf(" %d",ans_table[row][col]+1);
		}
		printf("\n");
	}

	return 0;
}

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


int main(){
    int n;
    cin >> n;
    int N = n*n;
    Graph G(N);
    for(int i=0; i<2*n*(n-1); i++){
        int a, b;
        cin >> a >> b;
        a--; b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    Graph H(n, vector<int>(n, -1));
    vector<int> used(N, 0);
    for(int i=0; i<N; i++){
        if(G[i].size() == 2){
            H[0][0] = i;
            used[i] = 1;
            break;
        }
    }

    for(int i=0; i<n-1; i++){
        for(auto &&k : G[H[0][i]]){
            if(G[k].size() <= 3 && !used[k]){
                H[0][i+1] = k;
                used[k] = 1;
                break;
            }
        }
    }

    for(int j=0; j<n-1; j++){
        for(int i=0; i<n; i++){
            for(auto &&k : G[H[j][i]]){
                if(used[k]) continue;
                H[j+1][i] = k;
                used[k] = 1;
            }
            if(H[j+1][i] == -1){
                cout << "!" << H[j][i] << endl;
            }
        }
    }

    for(int j=0; j<n; j++){
        for(int i=0; i<n; i++){
            cout << H[j][i]+1 << (i==n-1 ? "\n":" ");
        }
    }

}