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;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T>& V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> >& Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T>& mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }
template<typename T>vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type fill_v(T &t,const V &v){t=v;}
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type fill_v(T &t,const V &v){for(auto &e:t) fill_v(e,v);}
/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

template<class Type>
Type solve(Type res = Type()){
    int N; cin >> N;
    vector<vector<int>> G(N*N);
    vector<int> used(N*N);
    vector<int> outdeg(N*N);
    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);
        
        outdeg[a]++;
        outdeg[b]++;
    }
    res = vector<vector<int>>(N,vector<int>(N,-1));
    int u = (int)(min_element(outdeg.begin(), outdeg.end())-outdeg.begin());
    res[0][0] = u;
    res[0][1] = G[u][0]; res[1][0] = G[u][1];
    
    used[u] = used[G[u][0]] = used[G[u][1]] = 1;
    
    // 一列目と一行目は矛盾が生じないように揃える
    for(int j = 2; j < N;j++){
        if(j != N-1){
            for(auto v:G[res[0][j-1]]) if(!used[v] && outdeg[v] == 3) { res[0][j] = v; used[v] = true; break; }
        }else{
            for(auto v:G[res[0][j-1]]) if(!used[v] && outdeg[v] == 2) { res[0][j] = v; used[v] = true; break; }
        }
    }
    for(int i = 2; i < N;i++){
        if(i != N-1){
            for(auto v:G[res[i-1][0]]) if(!used[v] && outdeg[v] == 3) { res[i][0] = v; used[v] = true; break; }
        }else{
            for(auto v:G[res[i-1][0]]) if(!used[v] && outdeg[v] == 2) { res[i][0] = v; used[v] = true; break; }
        }
    }
    // 残りを埋めていく
    for(int i = 1; i < N;i++){
        for(int j = 1; j < N;j++){
            for(auto cand:G[res[i-1][j]]){
                if(used[cand]) continue;
                res[i][j] = cand;
                used[cand] = true;
                break;
            }
        }
    }
    
    for(auto& vec:res) for(auto& v:vec) v++;
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    // solve(0);
    cout << fixed << setprecision(15) << solve<vector<vector<int>>>() << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int INF = 1000000;
int main(){
  int N;
  cin >> N;
  int M = 2 * N * N - 2 * N;
  vector<vector<int>> E(N * N);
  for (int i = 0; i < M; i++){
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    E[a].push_back(b);
    E[b].push_back(a);
  }
  int s = -1, t = -1;
  for (int i = 0; i < N * N; i++){
    if (E[i].size() == 2){
      s = i;
      break;
    }
  }
  vector<int> d1(N * N, INF);
  d1[s] = 0;
  queue<int> Q1;
  Q1.push(s);
  while (!Q1.empty()){
    int v = Q1.front();
    Q1.pop();
    for (int w : E[v]){
      if (d1[w] == INF){
        d1[w] = d1[v] + 1;
        Q1.push(w);
        if (E[w].size() == 2 && t == -1){
          t = w;
        }
      }
    }
  }
  vector<int> d2(N * N, INF);
  d2[t] = 0;
  queue<int> Q2;
  Q2.push(t);
  while (!Q2.empty()){
    int v = Q2.front();
    Q2.pop();
    for (int w : E[v]){
      if (d2[w] == INF){
        d2[w] = d2[v] + 1;
        Q2.push(w);
      }
    }
  }
  vector<vector<int>> ans(N, vector<int>(N));
  for (int i = 0; i < N * N; i++){
    ans[(d1[i] + d2[i] - N + 1) / 2][(d1[i] - d2[i] + N - 1) / 2] = i + 1;
  }
  for (int i = 0; i < N; i++){
    for (int j = 0; j < N; j++){
      cout << ans[i][j];
      if (j < N - 1){
        cout << ' ';
      }
    }
    cout << endl;
  }
}