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>
using namespace std;

using Graph=vector<vector<int>>;

int main(){
    int n;
    cin>>n;
    int sz=n*n;
    Graph g(sz);
    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);
    } 

    vector<vector<int>> res(n,vector<int>(n,-1));
    
    if(n==2){
        res[0][0]=0;
        res[0][1]=g[0][0];
        res[1][0]=g[0][1];
        for(int i=1;i<4;i++){
            if(res[0][1]!=i && res[1][0]!=i){
                res[1][1]=i;
                break;
            }
        }
    }
    else{
        int lu=-1;
        for(int i=0;i<sz;i++){
            if(g[i].size()==2){
                lu=i;
                break;
            }
        }
        assert(lu!=-1);

        res[0][0]=lu;
        for(int i=1;i<n;i++){
            int pre=res[0][i-1];
            for(auto to:g[pre]){
                if(g[to].size()<=3 && (i==1 || res[0][i-2]!=to)){
                    res[0][i]=to;
                    break;
                }
            }
        }
        for(int i=1;i<n;i++){
            int pre=res[i-1][0];
            for(auto to:g[pre]){
                if(to!=res[0][i] && g[to].size()<=3 && (i==1 || res[i-2][0]!=to)){
                    res[i][0]=to;
                    break;
                }
            }
        }

        for(int i=1;i<n;i++){
            for(int j=1;j<n;j++){
                int v0=res[i-1][j];
                int v1=res[i][j-1];
                int notV=res[i-1][j-1];
                for(auto to0:g[v0]){
                    for(auto to1:g[v1]){
                        if(to0==to1 && to0!=notV){
                            res[i][j]=to0;
                        }
                    }
                }
            }
        }
    }

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

C++: #include<iostream>
#include<string>
#include<iomanip>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
#define int short
#define MAX 110


int dxy[4] = {-1, 1, 0, 0};
int dd[] = {0,1};
int N, a, b, ans[MAX][MAX] = {};
set<int> R[MAX*MAX];

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

inline void hoge(int y, int x){//cout<<y<<" "<<x<<endl;
	if(y == 1){
		for(int z : R[ans[y][x-1]]){
			//cout<<z<<" -----"<<endl;
			if(R[z].size() == 4)continue;
			ans[y][x] = z;
		}
	} else {
		for(int z : R[ans[y-1][x]]){
			//cout<<z<<" -----"<<endl;
			ans[y][x] = z;
		}
	}
	
	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(ans[y][x]);
		R[ans[y][x]].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;
		for(int i = 0; i < 2; i++){
			int ny = y + dd[i], nx = x + dd[1-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>>b;
		R[a].insert(b);
		R[b].insert(a);
		if(R[a].size() == 2) ans[1][1] = a;
		if(R[b].size() == 2) ans[1][1] = b;
	}
	
	huga();
	
	for(int i = 1; i <= N; i++){
		for(int j = 1; j <= N; j++){
			if(j!=1)cout<<" ";
			cout<<ans[i][j];
		}cout<<endl;
	}
	
	
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
	int n;
	cin >> n;
	vector<vector<int> > adj(n*n);
	for(int i=0; i<2*n*n-2*n; i++){
		int a,b;
		cin >> a >> b;
		a--; b--;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}

	vector<vector<int> > ans(n, vector<int>(n));
	for(int i=0; i<n*n; i++){
		if((int)adj[i].size() == 2){
			ans[0][0] = i;
			ans[0][1] = adj[i][0];
			ans[1][0] = adj[i][1];
			break;
		}
	}
	for(int i=2; i<n; i++){
		for(int next: adj[ans[0][i-1]]){
			if(next != ans[0][i-2] and (int)adj[next].size() < 4){
				ans[0][i] = next;
				break;
			}
		}
		for(int next: adj[ans[i-1][0]]){
			if(next != ans[i-2][0] and (int)adj[next].size() < 4){
				ans[i][0] = next;
				break;
			}
		}
	}

	for(int i=1; i<n; i++){
		for(int j=1; j<n; j++){
			for(int k: adj[ans[i][j-1]]){
				for(int l: adj[ans[i-1][j]]){
					if(k == l and k != ans[i-1][j-1]){
						ans[i][j] = k;
					}
				}
			}
		}
	}
	for(int i=0; i<n; i++){
		for(int j=0; j<n; j++){
			cout << ans[i][j]+1 << " ";
		}
		cout << endl;
	}
	return 0;
}