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 PLL = pair<int, int>;
template <typename T> using V = vector<T>;

V<V<int>> edges;
V<V<int>> ans;
V<PLL> pos;
set<int> outside;
V<int> next_nodes;
    
int N;

int dh[] = { 1, 0, -1, 0 };
int dw[] = { 0, 1, 0, -1 };

bool check_pos(int h, int w) {
    return 0 <= h && h < N &&
           0 <= w && w < N;
}

bool check_outside(int h, int w) {
    return check_pos(h, w) &&
           ((h == 0 || h == N - 1) ||
            (w == 0 || w == N - 1)); 
}

bool check_used(int h, int w) { return ans[h][w] != 0; }
bool check_used(int num) { return pos[num] != PLL(-1, -1); }

void calc_outside(int num, int h, int w) {
    ans[h][w] = num;
    pos[num] = PLL(h, w);
    next_nodes.push_back(num);
    int nxt_num = -1;
    for(int nxt : edges[num]) {
        if(check_used(nxt)) continue;
        if(outside.find(nxt) == outside.end()) continue;
        nxt_num = nxt;
        break;
    }
    if(nxt_num == -1) return;
    for(int i = 0; i < 4; i++) {
        int nh = h + dh[i];
        int nw = w + dw[i];
        if(!check_outside(nh, nw) || check_used(nh, nw)) continue;
        calc_outside(nxt_num, nh, nw);
        break;
    }
}

void calc_inside() {
    V<int> next_next_nodes;
    for(int num : next_nodes) {
        int nxt_cand = -1;
        for(int nxt : edges[num]) {
            if(check_used(nxt)) continue;
            nxt_cand = nxt;
            break;
        }
        if(nxt_cand == -1) continue;
        int h, w;
        tie(h, w) = pos[num];
        for(int i = 0; i < 4; i++) {
            int nh = h + dh[i];
            int nw = w + dw[i];
            if(!check_pos(nh, nw) || check_used(nh, nw)) continue;
            next_next_nodes.push_back(nxt_cand);
            pos[nxt_cand] = PLL(nh, nw);
            ans[nh][nw] = nxt_cand;
            break;
        }
    }
    next_nodes = move(next_next_nodes);
}

int main() {
    cin >> N;
    int pow2 = N * N + 1;
    edges.resize(pow2);
    pos.resize(pow2, PLL(-1, -1));
    ans.resize(N, V<int>(N, 0));
    ans = V<V<int>>(N, V<int>(N, 0));
    for(int i = 0; i < 2 * N * N - 2 * N; i++) {
        int a, b;
        cin >> a >> b;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
    
    int init;
    for(int i = 1; i <= N * N; i++) if(edges[i].size() <= 3) {
        outside.insert(i);
        if(edges[i].size() == 2) init = i;
    }

    calc_outside(init, 0, 0);
    while(next_nodes.size()) calc_inside();

    for(auto &&ele : ans) for(int i = 0; i < N; i++) cout << ele[i] << " \n"[i + 1 == N];
    return 0;
}

C++: #include <vector>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <iostream>
using namespace std;

int deg[100010];
vector<int> G[100010];
int ans[110][110], used[100010];
// set<int> adj[110];

int dx[] = {-1, 0};
int dy[] = {0, -1};

int main() {
  int N; cin >> 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);
    deg[a]++; deg[b]++;

    if(a > b) swap(a, b);
    // adj[a].insert(b);
    // adj[b].insert(a);
  }

  
  for(int i=0; i<N*N; i++) {
    // search deg[i] == 2
    if(deg[i] == 2) {
      // start
      for(int r=0; r<N; r++) {
        for(int c=0; c<N; c++) {
          if(r == 0 and c == 0) {
            ans[r][c] = i;
            used[i] = true;
          }
          else {
            map<int, int> cnt;
            int neighbor = 0;
            for(int k=0; k<2; k++) {
              int x = r + dx[k], y = c + dy[k];
              if(x < 0 or y < 0 or x >= N or y >= N) continue;
              int id = ans[x][y];
              neighbor++;
              for(auto to : G[id]) {
                cnt[to]++;
              }
            }

            for(auto x : cnt) {
              if(x.second != neighbor) continue;
              int target = -1;
              if     (r == 0 and c == N-1) target = 2;
              else if(r == 0 and c == 0  ) target = 2;
              else if(r == N-1 and c == 0  ) target = 2;
              else if(r == N-1 and c == N-1) target = 2;
              else if(r == 0 or r == N-1) target = 3;
              else if(c == 0 or c == N-1) target = 3;
              else target = 4;

              // fprintf(stderr, "(x, y) = (%d, %d), target = %d, deg = %d\n", r, c, target, deg[x.first]);
              if(deg[x.first] != target) continue;
              if(used[x.first]) continue;
              ans[r][c] = x.first;
              used[x.first] = true;
              break;
            }
          }
        }
      }
      break;
    }
  }

  for(int i=0; i<N; i++) {
    for(int j=0; j<N; j++) {
      cout << ans[i][j] + 1 << " \n"[j+1==N];
    }
  }
  return 0;
}