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++: #define  _CRT_SECURE_NO_WARNINGS
#pragma comment (linker, "/STACK:526000000")

#include "bits/stdc++.h"

using namespace std;
typedef string::const_iterator State;
#define eps 1e-11L
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL

#define MOD 998244353LL
#define seg_size 262144*2LL
#define pb push_back
#define mp make_pair
typedef long long ll;
#define REP(a,b) for(long long (a) = 0;(a) < (b);++(a))
#define ALL(x) (x).begin(),(x).end()

unsigned long xor128() {
	static unsigned long x = 123456789, y = 362436069, z = 521288629, w = time(NULL);
	unsigned long t = (x ^ (x << 11));
	x = y; y = z; z = w;
	return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

void init() {
	iostream::sync_with_stdio(false);
	cout << fixed << setprecision(20);
}
#define int ll

vector<int> vertexs[20000];
int used[20000];
int ans[100][100];
void solve() {
	int n;
	cin >> n;
	REP(i, 2 * n * n - 2 * n) {
		int a, b;
		cin >> a >> b;
		vertexs[a].push_back(b);
		vertexs[b].push_back(a);
	}
	int starter = 0;
	REP(i, n * n + 1) {
		if (vertexs[i].size() == 2) {
			starter = i;
			break;
		}
	}
	ans[0][0] = starter;
	used[starter] = 1;
	int xe[4] = { 0,1,0,-1 };
	int ye[4] = { 1,0,-1,0 };
	pair<int, int> now_itr = mp(0, 0);
	int dir = 0;
	REP(q, 3 + (n - 2) * 4) {
		int next = 0;
		for (auto x : vertexs[starter]) {
			if (used[x] == 0&&vertexs[x].size() >= 1 && vertexs[x].size() <= 3) {
				next = x;
				break;
			}
		}
		while (true) {
			pair<int, int> next_go = mp(now_itr.first + xe[dir], now_itr.second + ye[dir]);
			if (next_go.first >= 0 && next_go.first < n && next_go.second >= 0 && next_go.second < n) {
				now_itr = next_go;
				break;
			}
			dir++;
		}
		ans[now_itr.first][now_itr.second] = next;
		starter = next;
		used[starter] = 1;
	}
	for (int i = 1; i < n - 1; ++i) {
		for (int q = 1; q < n - 1; ++q) {
			map<int, int> cnter;
			for (auto x : vertexs[ans[i - 1][q]]) {
				cnter[x]++;
			}
			for (auto x : vertexs[ans[i][q - 1]]) {
				cnter[x]++;
			}
			for (auto x : cnter) {
				if (used[x.first] == 0 && x.second >= 2) {
					used[x.first] = 1;
					ans[i][q] = x.first;
					break;
				}
			}
		}
	}
	REP(i, n) {
		REP(q, n) {
			if (q != 0) cout << " ";
			cout << ans[i][q];
		}
		cout << endl;
	}
}

#undef int
int main() {
	init();
	solve();
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;

int ans[101][101];
vector<int> g[10001];
bool used[10001];
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
int main(){
    int n,ai,bi;
    cin>>n;
    int m=2*n*n-2*n;
    rep(i,m){
        cin>>ai>>bi;
        --ai,--bi;
        g[ai].pb(bi);
        g[bi].pb(ai);
    }
    int cur=0;
    rep(i,m){
        if(g[i].size()==2){
            cur=i;break;
        }
    }
    int tx=0,ty=0,id=0;
    while(1){
        ans[ty][tx]=cur+1;
        used[cur]=true;
        ty=ty+dy[id],tx=tx+dx[id];
        rep(i,g[cur].size()){
            if(g[g[cur][i]].size()==2&&!used[g[cur][i]]){
                cur=g[cur][i];
                ++id;
                break;
            }
            else if(g[g[cur][i]].size()==3&&!used[g[cur][i]]){
                cur=g[cur][i];break;
            }
        }
        if(tx==0&&ty==0) break;
    }
    FOR(i,1,n-1)FOR(j,1,n-1){
        int t1=ans[i-1][j]-1,t2=ans[i][j-1]-1;
        map<int,int> mp;
        rep(k,g[t1].size()) ++mp[g[t1][k]];
        rep(k,g[t2].size()) ++mp[g[t2][k]];
        for(auto it:mp)if(it.second==2&&!used[it.first]){
            ans[i][j]=it.first+1;used[it.first]=true;
            break;
        }
    }
    rep(i,n){
        rep(j,n) cout<<ans[i][j]<<" ";
        cout<<endl;
    }
    return 0;
}