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"

#define REP(i,n) for(ll i=0;i<ll(n);++i)
#define RREP(i,n) for(ll i=ll(n)-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<ll(n);++i)
#define RFOR(i,m,n) for(ll i=ll(n)-1;i>=ll(m);--i)
#define ALL(v) (v).begin(),(v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

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


using namespace std;

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <class T> bool chmin(T & a, T b) { if (a > b) { a = b; return true; } return false; }
template <class T> bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n; cin >> n;

	vvi v(n, vi(n, -1));
	int m = 2 * n * n - 2 * n;
	vi a(m), b(m);
	vvi cnt(n * n);
	REP(i, m) {
		cin >> a[i] >> b[i];
		a[i]--; b[i]--;
		cnt[a[i]].push_back(b[i]);
		cnt[b[i]].push_back(a[i]);
	}
	vi used(m, 0);
	REP(i, n)REP(j, n) {
		int c = 2;
		if (i % (n - 1) != 0)c++;
		if (j % (n - 1) != 0)c++;
		REP(k, n * n) {
			if (cnt[k].size() != c || used[k] == 1)continue;
			bool ok = true;
			if (i > 0)if (find(ALL(cnt[k]), v[i - 1][j]) == cnt[k].end())ok = false;
			if (j > 0)if (find(ALL(cnt[k]), v[i][j - 1]) == cnt[k].end())ok = false;
			if (ok) {
				used[k] = 1;
				v[i][j] = k;
				break;
			}
		}
	}
	REP(i, n)REP(j, n) {
		cout << v[i][j] + 1 << " \n"[j == n - 1];
	}
}

C++: #include<iostream>
#include<string>
#include<iomanip>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#include<unistd.h>
using namespace std;
#define MAX 110

//ものすごくアホだった(3回目)


int dxy[4] = {-1, 1, 0, 0};
int dd[] = {0,1};
int N, a, b, ans[MAX][MAX] = {};
vector<int> R[MAX*MAX];


template<typename T>
void remove(vector<T>& vector, unsigned int index)
{
    vector.erase(vector.begin() + index);
}

inline bool is_out_of_range(int z){
	return (z <= 0 || N < z) ;
}

inline void hoge(int y, int x){
	 //cout<<y<<" "<<x<<endl; 
	// sleep(2);
	//string hogehoge;
	//cin>>hogehoge;
	if(y == 1){
		for(int z : R[ans[y][x-1]]){
			// cout<<z<<" -----"<<endl;
			if(R[z].size() == 4)continue;
			ans[y][x] = z;
		}
	} else {
		for(int z : R[ans[y-1][x]]){
			// cout<<z<<" -----"<<endl;
			ans[y][x] = z;
		}
	}
	// cout<<ans[y][x]<<" ans "<<endl;
	
	for(int i = 0; i < 4; i++){
		int ny = y + dxy[i], nx = x + dxy[3-i];
		if(is_out_of_range(ny)||is_out_of_range(nx)) continue;
		if(ans[ny][nx] == 0)continue;
		for(int i = 0; i < R[ans[ny][nx]].size(); i++){
			if(R[ans[ny][nx]][i] == ans[y][x]) remove(R[ans[ny][nx]],i);
		}
		for(int i = 0; i < R[ans[y][x]].size(); i++){
			if(R[ans[y][x]][i] == ans[ny][nx]) remove(R[ans[y][x]],i);
		}
		
		// R[ans[ny][nx]].erase(ans[y][x]);
		// R[ans[y][x]].erase(ans[ny][nx]);
	}
}

int huga(){
	queue<pair<int,int>> Q;
	
	Q.push(make_pair(1,1));
	
	while(!Q.empty()){
		pair<int,int> P = Q.front();Q.pop();//string hogehoge; cin>>hogehoge;
		int y = P.first, x = P.second;
		for(int i = 0; i < 2; i++){
			int ny = y + dd[i], nx = x + dd[1-i];
			if(is_out_of_range(ny)||is_out_of_range(nx)) continue;
			if(ans[ny][nx]) continue;
			Q.push(make_pair(ny,nx));
			hoge(ny,nx);
		}
	}
}

signed main(){

	
	cin>>N;
	
	for(int i = 1; i <= 2*N*N-2*N; i++){
		cin>>a>>b;
		R[a].push_back(b);
		R[b].push_back(a);
		
		// if(a==950) cout<<"a "<<b<<endl;
		// if(b==950) cout<<"b "<<a<<endl;
	}
	for(int i = 1; i <= N*N; i++){
		if(R[i].size() == 2) ans[1][1] = i;
	}
	
	// cout<<ans[1][1]<<" "<<R[ans[1][1]].size()<<endl;
	huga();
	
	for(int i = 1; i <= N; i++){
		for(int j = 1; j <= N; j++){
			if(j!=1)cout<<" ";
			cout<<ans[i][j];
		}cout<<endl;
	}
	
	
	return 0;
}

C++: #include<iostream>
#include<string>
#include<map>
#include<set>
#include<vector>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
bool used[200000];
int ans[101][101],idx,s1,s2;
int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0},n;
vector<int>v[20000];
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;
  }
}