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;
int ans[100][100];
set<int> g[10000];
bool f[10000];
int main()
{
	int n;
	fill(ans[0],ans[100],-1);
	fill(f,f+10000,false);
	cin>>n;
	for(int i=0;i<2*n*n-2*n;i++)
	{
		int a,b;
		cin>>a>>b;
		a--;b--;
		g[a].insert(b);
		g[b].insert(a);
	}
	//左上の頂点を探す
	for(int i=0;i<n*n;i++)
	{
		if(g[i].size()==2)
		{
			ans[0][0]=i;
			f[i]=true;
			break;
		}
	}
	//1行目
	for(int j=1;j<n;j++)
	{
		for(int k=0;k<n*n;k++)
		{
			if(f[k])
				continue;
			if(j==n-1)
			{
				if(g[k].size()==2 && g[k].count(ans[0][j-1])==1)
				{
					ans[0][j]=k;
					f[k]=true;
					break;
				}
			}else{
				if(g[k].size()==3 && g[k].count(ans[0][j-1])==1)
				{
					ans[0][j]=k;
					f[k]=true;
					break;
				}
			}
		}
	}
	//2行目～n-1行目
	for(int i=1;i<n-1;i++)
	{
		for(int j=0;j<n;j++)
		{
			for(int k=0;k<n*n;k++)
			{
				if(f[k])
					continue;
				if(j==0)
				{
					if(g[k].size()==3 && g[k].count(ans[i-1][j])==1)
					{
						ans[i][j]=k;
						f[k]=true;
						break;
					}
				}else if(j==n-1){
					if(g[k].size()==3 && g[k].count(ans[i-1][j])==1 && g[k].count(ans[i][j-1])==1)
					{
						ans[i][j]=k;
						f[k]=true;
						break;
					}
				}else{
					if(g[k].size()==4 && g[k].count(ans[i][j-1])==1 && g[k].count(ans[i-1][j])==1)
					{
						ans[i][j]=k;
						f[k]=true;
						break;
					}
				}
			}
		}
	}
	//n行目
	for(int j=0;j<n;j++)
	{
		for(int k=0;k<n*n;k++)
		{
			if(f[k])
				continue;
			if(j==0)
			{
				if(g[k].size()==2 && g[k].count(ans[n-2][j])==1)
				{
					ans[n-1][j]=k;
					f[k]=true;
					break;
				}
			}else if(j==n-1){
				if(g[k].size()==2 && g[k].count(ans[n-1][j-1])==1 && g[k].count(ans[n-2][j]))
				{
					ans[n-1][j]=k;
					f[k]=true;
					break;
				}
			}else{
				if(g[k].size()==3 && g[k].count(ans[n-1][j-1])==1 && g[k].count(ans[n-2][j]))
				{
					ans[n-1][j]=k;
					f[k]=true;
					break;
				}
			}
		}
	}
	//output
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			cout<<ans[i][j]+1;
			if(j<n-1)
				cout<<" ";
		}
		cout<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
vector<vector<int>> p;
vector<vector<int>> ans;
int main() {
  cin >> n;
  p.resize(n * n);
  ans.resize(n);
  for (int i = 0; i < n; i++)
    ans[i].resize(n);

  for (int i = 0; i < 2 * n * n - 2 * n; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    p[a].push_back(b);
    p[b].push_back(a);
  }
  int now = -1;
  int m = n * n;
  vector<bool> used(m);

  for (int i = 0; i < n; i++) {
    int nowlim = 2;
    bool ok = false;
    if (i == 0)
      ok = true;
    if (i == n - 1) {
      nowlim--;
    }
    for (int k = 0; k < m; k++) {
      int c = 0;
      bool contains = ok;
      if (used[k])
        continue;
      for (int &v : p[k]) {
        if (!used[v])
          c++;
        else {
          if (!contains) {
            if (v == ans[i - 1][0])
              contains = true;
          }
        }
      }

      if (c == nowlim && contains)
        now = k;
    }

    int lim = 2, ulim = 2;
    if (i == 0)
      ulim--;
    if (i == n - 1)
      lim--;

    for (int j = 0; j < n; j++) {
      ans[i][j] = now;
      used[now] = true;
      if (j == n - 1)
        break;
      if (j == n - 2)
        lim--;
      for (auto &v : p[now]) {
        if (used[v])
          continue;
        int c = 0, d = 0;
        for (auto &w : p[v])
          if (!used[w])
            c++;
          else
            d++;
        if (c == lim && d == ulim) {
          now = v;
          break;
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << ans[i][0] + 1;
    for (int j = 1; j < n; j++)
      cout << " " << ans[i][j] + 1;
    cout << endl;
  }

  return 0;
}