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 <iostream>
#include <queue>
#include <cstring>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
#include <limits.h>
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef long long ll;
typedef vector<ll> vll;
const int inf=10000000;
int n;
bool visited[10005];
int deg[10005];
vi deglist[3];
vi g[10005];
int ans[105][105];
typedef pair<int,int> P;
typedef pair<int,pair<int,int> > PP;

int main(int argc,char const* argv[])
{
  cin >> n;
  memset(visited,false,sizeof(visited));
  memset(deg,0,sizeof(deg));
  memset(ans,0,sizeof(ans));
  for(int i=0;i<2*n*(n-1);i++)
    {
      int a,b;
      cin >> a >> b;
      a--;
      b--;
      deg[a]++;
      deg[b]++;
      g[a].push_back(b);
      g[b].push_back(a);
    }
  for(int i=0;i<10005;i++)
    {
      if(deg[i]==2) deglist[0].push_back(i);
      else if(deg[i]==3) deglist[1].push_back(i);
      else if(deg[i]==4) deglist[2].push_back(i);
    }
  int twodind=0;
  ans[0][0]=deglist[0][twodind];
  int pretwo=ans[0][0];
  twodind++;
  if(n==2)
    {
      ans[0][1]=g[pretwo][0];
      visited[g[pretwo][0]]=true;;
      ans[1][0]=g[pretwo][1];
      visited[g[pretwo][1]]=true;
      for(int i=0;i<deglist[0].size();i++)
	{
	  if(!visited[deglist[0][i]])
	    {
	      ans[1][1]=deglist[0][i];
	      break;
	    }
	}
    }
  for(int x=1;x<n;x++)
    {
      int prev=ans[0][x-1];
      if(x!=n-1)
	{
	  for(int k=0;k<g[prev].size();k++)
	    {
	      if(deg[g[prev][k]]==3 && !visited[g[prev][k]])
		{
		  ans[0][x]=g[prev][k];
		  visited[g[prev][k]]=true;
		  //g[prev].erase(g[prev].begin()+k);
		  break;
		}
	    }
	}
      else
	{
	  for(int k=0;k<g[prev].size();k++)
	    {
	      if(deg[g[prev][k]]==2 &&!visited[g[prev][k]])
		{
		  ans[0][x]=g[prev][k];
		  visited[g[prev][k]]=true;
		  //g[prev].erase(g[prev].begin()+k);
		}
	    }
	}
    }
  for(int y=1;y<n;y++)
    {
      int prev=ans[y-1][n-1];
      if(y!=n-1)
	{
	  for(int k=0;k<g[prev].size();k++)
	    {
	      if(deg[g[prev][k]]==3 && !visited[g[prev][k]])
		{
		  ans[y][n-1]=g[prev][k];
		  visited[g[prev][k]]=true;
		  //g[prev].erase(g[prev].begin()+k);
		  break;
		}
	    }
	}
      else
	{
	  for(int k=0;k<g[prev].size();k++)
	    {
	      if(deg[g[prev][k]]==2 && !visited[g[prev][k]])
		{
		  ans[y][n-1]=g[prev][k];
		  visited[g[prev][k]]=true;
		  //g[prev].erase(g[prev].begin()+k);
		}
	    }
	}
    }
  for(int x=n-2;x>=0;x--)
    {
      int prev=ans[n-1][x+1];
      if(x!=0)
	{
	  for(int k=0;k<g[prev].size();k++)
	    {
	      if(deg[g[prev][k]]==3 &&!visited[g[prev][k]])
		{
		  ans[n-1][x]=g[prev][k];
		  visited[g[prev][k]]=true;
		  //g[prev].erase(g[prev].begin()+k);
		  break;
		}
	    }
	}
      else
	{
	  for(int k=0;k<g[prev].size();k++)
	    {
	      if(deg[g[prev][k]]==2 &&!visited[g[prev][k]])
		{
		  ans[n-1][x]=g[prev][k];
		  visited[g[prev][k]]=true;
		  //g[prev].erase(g[prev].begin()+k);
		}
	    }
	}
    }
  for(int y=n-2;y>=0;y--)
    {
      int prev=ans[y+1][0];
      if(y!=0)
	{
	  for(int k=0;k<g[prev].size();k++)
	    {
	      if(deg[g[prev][k]]==3 &&!visited[g[prev][k]])
		{
		  ans[y][0]=g[prev][k];
		  visited[g[prev][k]]=true;
		  //g[prev].erase(g[prev].begin()+k);
		  break;
		}
	    }
	}
      else
	{
	  for(int k=0;k<g[prev].size();k++)
	    {
	      if(deg[g[prev][k]]==2 && !visited[g[prev][k]])
		{
		  ans[y][0]=g[prev][k];
		  visited[g[prev][k]]=true;
		  //g[prev].erase(g[prev].begin()+k);
		}
	    }
	}
    }
  if(n>=3)
    {
      for(int y=1;y<n-1;y++)
	{
	  for(int x=1;x<n-1;x++)
	    {
	      int prev1=ans[y-1][x];
	      int prev2=ans[y][x-1];
	      bool get=false;
	      for(int k1=0;k1<g[prev1].size();k1++)
		{
		  for(int k2=0;k2<g[prev2].size();k2++)
		    {
		      if(g[prev1][k1]==g[prev2][k2] && !visited[g[prev1][k1]])
			{
			  ans[y][x]=g[prev1][k1];
			  visited[g[prev1][k1]]=true;
			  get=true;
			  break;
			}
		    }
		  if(get) break;
		}
	    }
	}
    }
  for(int i=0;i<n;i++)
    {
      for(int j=0;j<n;j++)
	{
	  if(j!=n-1)
	    {
	      cout << ans[i][j]+1 << ' ';
	    }
	  else
	    {
	      cout << ans[i][j]+1 << endl;
	    }
	}
    }
    
  return 0;
}