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 <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int n;
    cin >> n;
    vector<vector<int>> g(n+2, vector<int> (n+2, 0));
    auto h = g;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            g[i+1][j+1] = 1;
        }
    }
    array<int, 4> di = {1, 0, -1, 0}, dj = {0, 1, 0, -1};
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= n; ++j) {
            for (int k = 0; k < 4; ++k) {
                h[i][j] += g[i+di[k]][j+dj[k]];
            }
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            g[i+1][j+1] = 0;
        }
    }
    vector<int> v(n*n);
    int M = 2*(n*n-n);
    vector<int> a(M), b(M);
    vector<vector<int>> G(n*n);
    for (int i = 0; i < M; ++i) {
        cin >> a[i] >> b[i];
        a[i]--; b[i]--;
        v[a[i]]++; v[b[i]]++;
        G[a[i]].emplace_back(b[i]);
        G[b[i]].emplace_back(a[i]);
    }
    int cur = 0;
    for (int i = 0; i < n*n; ++i) {
        if(v[i] == 2) cur = i;
    }
    g[1][1] = cur+1;
    v[cur] = -1;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if(g[i+1][j+1]) for (int to : G[g[i+1][j+1]-1]) {
                for (int k = 0; k < 2; ++k) {
                    int y = i+di[k]+1, x = j+dj[k]+1;
                    if(!g[y][x] && v[to] == h[y][x]){
                        g[y][x] = to+1;
                        v[to] = -1;
                    }
                }
            }

        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if(j) cout << " ";
            cout << g[i+1][j+1];
        }
        puts("");
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i))
#define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T> bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}
template<class T> T div_floor(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>=0 ? a/b : (a+1)/b-1;
}
template<class T> T div_ceil(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>0 ? (a-1)/b+1 : a/b;
}

constexpr lint mod = 1e9+7;
constexpr lint INF = mod * mod;
constexpr int MAX = 200010;

int main(){
    int n;
    scanf("%d", &n);
    vector<int> G[n*n];
    rep(i, 2*n*(n-1)){
        int a, b;
        scanf("%d%d", &a, &b);
        --a; --b;
        G[a].push_back(b);
        G[b].push_back(a);
    }

    int ans[n][n];
    int s;
    rep(i, n*n)if(G[i].size() == 2){s = i; break;}
    ans[0][0] = s;
    bool used[n*n];
    memset(used, false, sizeof(used));
    used[s] = true;
    for(int t=G[s][0], j=1; j<n; ++j){
        ans[0][j] = t;
        used[t] = true;
        for(int nt: G[t])if(G[nt].size() <= 3 && !used[nt]) t = nt;
    }
    for(int t=G[s][1], j=1; j<n; ++j){
        ans[j][0] = t;
        used[t] = true;
        for(int nt: G[t])if(G[nt].size() <= 3 && !used[nt]) t = nt;
    }

    For(i, 1, n)For(j, 1, n){
        for(int a: G[ans[i-1][j]])for(int b: G[ans[i][j-1]])if(a == b && !used[a]){
            ans[i][j] = a;
            used[a] = true;
        }
    }

    rep(i, n){
        rep(j, n) printf("%d ", ans[i][j] + 1);
        printf("\n");
    }
}