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 ll = long long;
using ull = unsigned long long;

const long double PI = acos(-1);
constexpr long double EPS = 1e-15;
constexpr int inf = 2e9;
constexpr ll INF = 2e18;
constexpr ll MOD = 1e9+7;
constexpr ll MOD1 = 998244353;
typedef pair<ll,ll> P;

#define all(v) (v).begin(), (v).end()
#define rep(i,a,b) for (int i = (a); i < (b); i++)
#define REP(i,n) rep(i,0,n)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
//#define mp make_pair

int ans[110][110];
set<int> st[10010];

int main(){
    int n;
    cin >> n;
    int k = 2 * n * n - 2 * n;
    REP(i,k) {
        int a,b;
        cin >> a >> b;
        st[a].insert(b);
        st[b].insert(a);
    }
    int beg = 0;
    REP(i,n*n) {
        if (sz(st[i+1]) == 2) {
            beg = i+1;
            break;
        }
    }
    ans[0][0] = beg;
    ans[0][1] = *st[beg].begin();
    int right = ans[0][1];
    st[beg].erase(right);
    st[right].erase(beg);
    ans[1][0] = *st[beg].begin();
    int down = ans[1][0];
    st[beg].erase(down);
    st[down].erase(beg);
    int left;
    int up;
    for (int i = 0; i < n - 1; i++) {
        if (i != 0) {
            if (ans[i+1][0] == 0)
                ans[i+1][0] = *st[ans[i][0]].begin();
            up = ans[i][0];
            down = ans[i+1][0];
            st[up].erase(down);
            st[down].erase(up);
        }
        for (int j = 1; j < n; j++) {
            if (i == 0 && j == 0) continue;
            //下
            up = ans[i][j];
            left = ans[i+1][j-1];
            set<int> tmp;
            set_intersection(all(st[up]), all(st[left]), inserter(tmp, tmp.end()));
            if (ans[i+1][j] == 0)
                ans[i+1][j] = *tmp.begin();
            down = ans[i+1][j];
            st[down].erase(up);
            st[down].erase(left);
            st[up].erase(down);
            st[left].erase(down);

            if (j != n-1) {
                //右
                left = ans[i][j];
                if (ans[i][j+1] == 0)
                    ans[i][j+1] = *st[left].begin();
                int now = ans[i][j+1];
                st[now].erase(left);
                st[left].erase(now);
                if (i != 0) {
                    up = ans[i-1][j+1];
                    st[up].erase(now);
                    st[now].erase(up);
                }
            }
        }
    }
    if (ans[n-1][n-1] == 0)
        ans[n-1][n-1] = *st[ans[n-1][n-2]].begin();
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << ans[i][j] << " \n"[j == n-1];
        }
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
vector<int>G[10001];
int cnt[101][101],n,a[101][101];
bool used[10001];
main()
{
	cin>>n;
	for(int a,b;cin>>a>>b;)
	{
		G[a].push_back(b);
		G[b].push_back(a);
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			for(int r=0;r<4;r++)
			{
				int tx=i+dx[r],ty=j+dy[r];
				cnt[i][j]+=tx>=0&&ty>=0&&tx<n&&ty<n;
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(i==0&&j==0)
			{
				int k;
				for(k=1;G[k].size()!=2;k++);
				used[k]=1;
				a[i][j]=k;
			}
			else
			{
				int c[10001]={},ret=0;
				for(int r=0;r<4;r++)
				{
					int tx=i+dx[r],ty=j+dy[r];
					if(tx<0||ty<0||tx>=n||ty>=n)continue;
					if(a[tx][ty]==0)continue;
					ret++;
					for(int u:G[a[tx][ty]])c[u]++;
				}
				for(int u=1;u<=n*n;u++)
				{
					if(cnt[i][j]!=G[u].size())continue;
					if(used[u])continue;
					if(c[u]<ret)continue;
					a[i][j]=u;
					used[u]=1;
					break;
				}
			}
		}
	}
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)cout<<a[i][j]<<(j==n-1?"\n":" ");
	}
}

C++: #include<bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
bool used[100000];
int ans[101][101],idx,s1,s2;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0},n;
vector<int>v[1000];
int main(){
  cin>>n;
  r(i,n*n*2-2*n){
    cin>>s1>>s2;
    v[s1].push_back(s2);
    v[s2].push_back(s1);
  }
  r(i,n*n+1)if(v[i].size()==2)idx=i;
  ans[0][0]=idx;
  used[idx]=1;
  r(i,n)r(j,n)if(!ans[i][j]){
    vector<int>p;
    r(k,4){
      int y=i+dy[k];
      int x=j+dx[k];
      if(y<0||x<0||y>=n||x>=n)continue;
      p.push_back(ans[y][x]);
    }
    r(k,n*n+1)if(v[k].size()==p.size()){
      if(!used[k]){
	int f=0;
	r(l,p.size())if(p[l]){
	  r(q,v[k].size()){
	    if(v[k][q]==p[l])break;
	    if(q==v[k].size()-1)f++;
	  }
	}
	if(!f){
	  ans[i][j]=k;
	  used[k]=1;
	  goto L;
	}
      }
    }L:;
  }
  r(i,n){
    r(j,n){
      if(j)cout<<' ';
      cout<<ans[i][j];
    }
    cout<<endl;
  }
}