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;

#define rep(i, n) for(int i = 0; i < (n); i++)
#define repp(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perr(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)

typedef long long LL;
typedef long double LD;

int main(){
  int n;
  cin >> n;
  if(n==2){
    int m=2*n*n-2*n;
    vector<vector<int>> e(n*n);
    int u,v;
    rep(i,m){
      cin >> u >> v;
      e[u-1].push_back(v-1);
      e[v-1].push_back(u-1);
    }
    vector<int> dist(n*n,-1);
    queue<int> q;
    q.push(0);
    dist[0]=0;
    while(!q.empty()){
      int now=q.front();
      q.pop();
      for(auto nx:e[now]){
        if(dist[nx]!=-1) continue;
        dist[nx]=dist[now]+1;
        q.push(nx);
      }
    }
    vector<vector<int>> ans(n,vector<int>(n,-1));
    rep(i,4){
      if(dist[i]==0) ans[0][0]=i;
      else if(dist[i]==2) ans[1][1]=i;
      else{
        if(ans[0][1]==-1) ans[0][1]=i;
        else ans[1][0]=i;
      }
    }
    rep(i,n){
      rep(j,n){
        cout << ans[i][j]+1;
        if(j<n-1) cout << " ";
        else cout << endl;
      }
    }
    return 0;
  }
  vector<vector<int>> e(n*n);
  int m=2*n*n-2*n;
  int u,v;
  rep(i,m){
    cin >> u >> v;
    e[u-1].push_back(v-1);
    e[v-1].push_back(u-1);
  }
  int s;
  rep(i,n*n){
    if(e[i].size()==2){
      s=i;
      break;
    }
  }
  vector<int> par(n*n,-1);
  vector<int> dist(n*n,-1);
  queue<int> q;
  q.push(s);
  dist[s]=0;
  par[s]=s;
  int t;
  while(!q.empty()){
    int now=q.front();
    q.pop();
    for(auto nx:e[now]){
      if(dist[nx]!=-1) continue;
      dist[nx]=dist[now]+1;
      par[nx]=now;
      if(dist[nx]==n-1&&e[nx].size()==2){
        t=nx;
        goto next;
      }
      q.push(nx);
    }
  }
  next:;
  vector<bool> usd(n*n,false);
  vector<vector<int>> ans(n,vector<int>(n,-1));
  per(i,n){
    ans[0][i]=t;
    usd[t]=true;
    t=par[t];
  }
  rep(i,n-1){
    rep(j,n){
      for(auto nx:e[ans[i][j]]){
        if(!usd[nx]){
          ans[i+1][j]=nx;
          usd[nx]=true;
          break;
        }
      }
    }
  }
  rep(i,n){
    rep(j,n){
      cout << ans[i][j]+1;
      if(j<n-1) cout << " ";
      else cout << endl;
    }
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

int main(){
    int n;
    cin >>n;

    int V = n*n;
    vector<vector<int>> G(V);
    rep(i, 2*n*n-2*n){
        int a,b;
        cin >>a >>b;
        --a;
        --b;
        G[a].pb(b);
        G[b].pb(a);
    }

    vector<vector<int>> x(n, vector<int>(n,-1));

    rep(i,V)if(G[i].size() == 2){
        x[0][0] = i;
        x[0][1] = G[i][0];
        x[1][0] = G[i][1];
        break;
    }

    for(int i=2; i<n; ++i){
        int pre = x[0][i-2];
        int v = x[0][i-1];
        for(int e:G[v]){
            if(G[e].size()<=3 && e!=pre) x[0][i] = e;
        }
    }

    for(int i=2; i<n; ++i){
        int pre = x[i-2][0];
        int v = x[i-1][0];
        for(int e:G[v]){
            if(G[e].size()<=3 && e!=pre) x[i][0] = e;
        }
    }

    for(int i=1; i<n; ++i)for(int j=1; j<n; ++j){
        int p = x[i-1][j], q = x[i][j-1], r = x[i-1][j-1];
        for(int np:G[p])for(int nq:G[q]){
            if(np==nq && np!=r) x[i][j] = np;
        }
    }

    rep(i,n)rep(j,n) cout << x[i][j]+1 << " \n"[j==n-1];
    return 0;
}