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;
 
const int vy[] = {0, 1, 0, -1}, vx[] = {1, 0, -1, 0};
 
const int INF = 1 << 30;
 
int main()
{
  int N;
  cin >> N;
 
  vector< int > war[10000];
  int in[10000] = {};
 
  for(int i = 0; i < 2 * N * N - 2 * N; i++) {
    int a, b;
    cin >> a >> b;
    --a, --b;
    war[a].emplace_back(b);
    war[b].emplace_back(a);
    in[a]++;
    in[b]++;
  }
 
  int mat[100][100] = {{}};
  for(int i = 0; i < N; i++) {
    for(int j = 0; j < N; j++) {
      for(int k = 0; k < 4; k++) {
        int nx = j + vx[k], ny = i + vy[k];
        if(nx < 0 || ny < 0 || nx >= N || ny >= N) continue;
        mat[i][j]++;
      }
    }
  }
 
  vector< int > vs[5];
  for(int i = 0; i < N * N; i++) {
    vs[in[i]].emplace_back(i);
  }
 
  int v[100][100], used[10000] = {};
  memset(v, -1, sizeof(v));
 
  v[0][0] = vs[2][0];
  v[0][1] = war[v[0][0]][0];
  v[1][0] = war[v[0][0]][1];
  used[v[0][0]] = true;
  used[v[0][1]] = true;
  used[v[1][0]] = true;
 
 
  for(int i = 0; i < N * N; i++) {
    for(int j = 0; j < N; j++) {
      for(int k = 0; k < N; k++) {
        if(v[j][k] != -1) {
          for(int l = 0; l < 4; l++) {
            int ny = j + vy[l], nx = k + vx[l];
            if(ny < 0 || nx < 0 || ny >= N || nx >= N) continue;
            if(v[ny][nx] == -1) {
              int ret = 0;
              for(auto &p : war[v[j][k]]) {
                if(used[p]) continue;
                if(in[p] != mat[ny][nx]) continue;
                ret++;
              }
              if(ret == 1) {
                for(auto &p : war[v[j][k]]) {
                  if(used[p]) continue;
                  if(in[p] != mat[ny][nx]) continue;
                  v[ny][nx] = p;
                  used[p] = true;
                }
              }
            }
          }
        }
      }
    }
  }
 
  for(int i = 0; i < N; i++) {
    for(int j = 0; j < N; j++) {
      assert(v[i][j] >= 0);
      if(j > 0) cout << " ";
      cout << v[i][j] + 1;
    }
    cout << endl;
  }
}

C++: #include<iostream>
#include<set>
using namespace std;
#define inRange(x,a,b) (a <= x && x < b)

int main(){
    int n;
    cin >> n;
    set<int> s[n*n+1];
    for(int i = 0; i < 2*n*n-2*n; i++){
        int a, b;
        cin >> a >> b;
        s[a].insert(b);
        s[b].insert(a);
    }
    int ans[n][n], next;
    for(int i = 1; i <= n*n; i++){
        if(s[i].size() != 2)    continue;
        ans[0][0] = i;
        for(int j : s[i]){
            s[j].erase(i);
            next = j;
        }
        ans[0][1] = next;
        int tmp = -1, mi = 4;
        for(int j : s[next]){
            s[j].erase(next);
            if(inRange(s[j].size(), 0, mi)){
                tmp = j;
                mi = s[tmp].size();
            }
        }
        next = tmp;
        break;
    }
    set<int> done; // ugly!
    done.insert(ans[0][0]);
    done.insert(ans[0][1]);
    for(int i = 0; i < n; i++){
        if(i){
            // ugly!
            for(int j : s[ans[i-1][0]])   if(done.count(j) == 0)  next = j;
        }
        for(int j = (i==0 ? 2 : 0); j < n; j++){
            ans[i][j] = next;
            done.insert(next);
            int tmp = -1, mi = 4;
            for(int k : s[next]){
                s[k].erase(next);
                if((i==0 || j==n-1 || s[ans[i-1][j+1]].count(k)) && inRange(s[k].size(), 0, mi)){
                    tmp = k;
                    mi = s[tmp].size();
                }
            }
            next = tmp;
        }
    }
    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<bits/stdc++.h>
using namespace std;

int main(){
  int n;
  cin>>n;
  int nn=2*n*n-2*n;
  vector<int> g[10001];
  for(int i=0;i<nn;i++){
    int a,b;
    cin>>a>>b;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  int k;
  for(int i=1;i<=n*n;i++){
    if(g[i].size()==2){
      k=i;
    }
  }
  
  int ma[111][111]={};
  int un[10010]={};
  ma[0][0]=k;
  un[k]=1;
  for(int i=0;i<n;i++){
    
    if(i){
      int tmp=ma[i-1][0];
      for(int j=0;j<g[tmp].size();j++){
	if(un[g[tmp][j]])continue;
	ma[i][0]=g[tmp][j];
	un[g[tmp][j]]=1;
	break;
      }
    }
    
    for(int j=1;j<n;j++){

      int ry=ma[i][j-1];
      if(i==0){
	for(int y=0;y<g[ry].size();y++){
	  if(un[g[ry][y]])continue;
	  if(g[g[ry][y]].size()>3)continue;
	  ma[i][j]=g[ry][y];
	  un[g[ry][y]]=1;
	  break;
	}
	continue;
      }
      int rx=ma[i-1][j];
      for(int x=0;x<g[rx].size();x++){
	for(int y=0;y<g[ry].size();y++){
	  if(un[g[rx][x]]||un[g[ry][y]])continue;
	  if(g[rx][x]!=g[ry][y])continue;
	  ma[i][j]=g[rx][x];
	  un[g[rx][x]]=1;
	}
      }   
    }
  }
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      if(j)cout<<" ";
      cout<<ma[i][j];
    }
    cout<<endl;
  }
  return 0;
}