Problem:
Cube Surface Puzzle
Given a set of six pieces, “Cube Surface Puzzle” is to construct a hollow cube with filled surface.
Pieces of a puzzle is made of a number of small unit cubes laid grid-aligned on a plane.
For a puzzle constructing a cube of its side length
n
,
unit cubes are on either of the following two areas.
Core (blue): A square area with its side length
n
−2.
    Unit cubes fill up this area.
Fringe (red): The area of width 1 unit forming the outer fringe of the core.
     Each unit square in this area may be empty or with a unit cube on it.
Each piece is connected with faces of its unit cubes.
Pieces can be arbitrarily rotated and either side of the pieces can
be inside or outside of the constructed cube.
The unit cubes on the core area should come in the centers of the
faces of the constructed cube.
Consider that we have six pieces in Fig. E-1 (The first dataset of Sample Input).
Then, we can construct a cube as shown in Fig. E-2.
Fig. E-1 Pieces from the first dataset of Sample Input
Fig. E-2 Constructing a cube
Mr. Hadrian Hex has collected a number of cube surface puzzles.
One day, those pieces were mixed together and he
cannot find yet from which six pieces he can construct a cube.
Your task is to write a program to help Mr. Hex, which judges whether we can construct a cube for a given set of pieces.
Input
The input consists of at most 200 datasets, each in the following format.
n
x
1,1
x
1,2
…
x
1,
n
x
2,1
x
2,2
…
x
2,
n
…
x
6
n
,1
x
6
n
,2
…
x
6
n
,
n
The first line contains an integer
n
denoting the length of one side of the  cube to be constructed (3 ≤
n
≤ 9,
n
is odd).
The following 6
n
lines give the six pieces.
Each piece is described in
n
lines.
Each of the lines corresponds to one grid row and each of the characters in
the line, either ‘X’ or ‘.’, indicates whether or not a unit cube is on
the corresponding unit square: ‘X’ means a unit cube is on the column and ‘.’ means none is there.
The core area of each piece is centered in the data for the piece.
The end of the input is indicated by a line containing a zero.
Output
For each dataset, output “
Yes
” if we can construct a cube, or “
No
” if we cannot.
Sample Input
5
..XX.
.XXX.
XXXXX
XXXXX
X....
....X
XXXXX
.XXX.
.XXX.
.....
..XXX
XXXX.
.XXXX
.XXXX
...X.
...X.
.XXXX
XXXX.
XXXX.
.X.X.
XXX.X
.XXXX
XXXXX
.XXXX
.XXXX
XX...
.XXXX
XXXXX
XXXXX
XX...
5
..XX.
.XXX.
XXXXX
XXXX.
X....
....X
XXXXX
.XXX.
.XXX.
.....
.XXXX
XXXX.
.XXXX
.XXXX
...X.
...X.
.XXXX
XXXX.
XXXX.
.X.X.
XXX.X
.XXXX
XXXXX
.XXXX
.XXXX
XX...
XXXXX
XXXXX
.XXXX
XX...
0
Output for the Sample Input
Yes
No

Submitted solutions:

code_snippet: #include <bits/stdc++.h>

using namespace std;
typedef long long ll;

struct jie{
    char s[10][10][10];
}mp[7];
char a[15][15][15];
int n;
int flag,us[15];
void dfs(int m){
    if(m>6){
        flag=1;
        return;
    }
    for(int k=1;k<=6;k++){
        if(us[k]==1) continue;
        us[k]=1;
        for(int i=1;i<=8;i++){
            if(k==1||k==3){
                int y=1,f=1;
                if(k==3) y=n;
                for(int x=1;x<=n;x++){
                    for(int z=1;z<=n;z++){
                        if(mp[m].s[i][x][z]=='X'&&a[x][y][z]=='X') f=0;
                    }
                }
                if(f==0) continue;
                for(int x=1;x<=n;x++){
                    for(int z=1;z<=n;z++){
                        if(mp[m].s[i][x][z]=='X') a[x][y][z]='X';
                    }
                }
                dfs(m+1);
                for(int x=1;x<=n;x++){
                    for(int z=1;z<=n;z++){
                        if(mp[m].s[i][x][z]=='X') a[x][y][z]='.';
                    }
                }
                if(flag==1) return;
            }

            if(k==2||k==4){
                int x=n,f=1;
                if(k==4) x=1;
                for(int y=1;y<=n;y++){
                    for(int z=1;z<=n;z++){
                        if(mp[m].s[i][y][z]=='X'&&a[x][y][z]=='X') f=0;
                    }
                }
                if(f==0) continue;
                for(int y=1;y<=n;y++){
                    for(int z=1;z<=n;z++){
                        if(mp[m].s[i][y][z]=='X') a[x][y][z]='X';
                    }
                }
                dfs(m+1);
                for(int y=1;y<=n;y++){
                    for(int z=1;z<=n;z++){
                        if(mp[m].s[i][y][z]=='X') a[x][y][z]='.';
                    }
                }
                if(flag==1) return;
            }

            if(k==5||k==6){
                int z=1,f=1;
                if(k==6) z=n;
                for(int x=1;x<=n;x++){
                    for(int y=1;y<=n;y++){
                        if(mp[m].s[i][x][y]=='X'&&a[x][y][z]=='X') f=0;
                    }
                }
                if(f==0) continue;
                for(int x=1;x<=n;x++){
                    for(int y=1;y<=n;y++){
                        if(mp[m].s[i][x][y]=='X') a[x][y][z]='X';
                    }
                }
                dfs(m+1);
                for(int x=1;x<=n;x++){
                    for(int y=1;y<=n;y++){
                        if(mp[m].s[i][x][y]=='X') a[x][y][z]='.';
                    }
                }
                if(flag==1) return;
            }
        }
        us[k]=0;
    }
}
int main()
{
    for(int i=1;i<=10;i++){
        for(int j=1;j<=10;j++){
            for(int k=1;k<=10;k++) a[i][j][k]='.';
        }
    }
    while(~scanf("%d",&n)&&n){
        int sum=0;
        memset(us,0,sizeof(us));
        for(int k=1;k<=6;k++){
            for(int i=1;i<=n;i++){
                scanf("%s",mp[k].s[1][i]+1);
                for(int j=1;j<=n;j++) if(mp[k].s[1][i][j]=='X') sum++;
            }
            for(int kk=2;kk<=4;kk++){
                for(int j=1;j<=n;j++){
                    for(int i=1;i<=n;i++){
                        mp[k].s[kk][j][i]=mp[k].s[kk-1][n-i+1][j];
                    }
                }
            }

            for(int kk=5;kk<=8;kk++){
                for(int i=1;i<=n;i++){
                    for(int j=1;j<=n;j++){
                        mp[k].s[kk][i][j]=mp[k].s[kk-4][i][n-j+1];
                    }
                }
            }
        }
        flag=0;
        //printf("%d\n",sum);
        if(sum==n*n*2+(n-2)*n*2+(n-2)*(n-2)*2){
            dfs(1);
        }
        if(flag==0) printf("No\n");
        else printf("Yes\n");
    }
    return 0;
}
/*
3
.X.
XXX
.X.
.X.
XXX
.X.
...
.X.
...
...
.X.
...
XXX
.X.
XXX
XXX
.X.
XXX

*/
accuracy: 1
time_ms: 290
memory_kb: 3396
final_rank: 4

code_snippet: // #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;

// #include "titan_cpplib/others/print.cpp"

bool solve() {
    int n;
    cin >> n;
    if (n == 0) return false;

    using S = vector<vector<int>>;
    vector<S> X(6);
    rep(i, 6) {
        S x(n, vector<int>(n, 0));
        rep(j, n) {
            string s; cin >> s;
            rep(k, n) {
                x[j][k] = s[k] == 'X' ? 1 : 0;
            }
        }
        X[i] = x;
    }

    int cnt = 0;
    rep(idx, 6) {
        set<pair<int, int>> seen;
        rep(i, n) rep(j, n) if (i == 0 || j == 0 || i == n-1 || j == n-1) {
            seen.insert({i, j});
        }
        for (auto [i, j] : seen) {
            if (X[idx][i][j]) cnt++;
        }
    }
    if (cnt != n*12-2*8) {
        cout << "No" << endl;
        return true;
    }

    vector<vector<vector<int>>> G(n, vector<vector<int>>(n, vector<int>(n, 0)));

    auto rotate = [&] (S &R) -> void {
        auto P = R;
        rep(i, n) R[i][n-1] = P[0][i];
        rep(i, n) R[n-1][n-1-i] = P[i][n-1];
        rep(i, n) R[i][0] = P[n-1][i];
        rep(i, n) R[0][n-1-i] = P[i][0];
    };

    auto flip = [&] (S &R) -> void {
        rep(j, n/2) swap(R[0][j], R[0][n-1-j]);
        rep(j, n/2) swap(R[n-1][j], R[n-1][n-1-j]);
        for (int i = 1; i < n-1; ++i) swap(R[i][0], R[i][n-1]);
    };

    vector<vector<tuple<int, int, int>>> pos(6);
    rep(i, n) rep(j, n) {
        if (i == 0 || j == 0 || i == n-1 || j == n-1) {
            pos[0].push_back({0, i, j});
            pos[1].push_back({n-1, i, j});
            pos[2].push_back({i, 0, j});
            pos[3].push_back({i, n-1, j});
            pos[4].push_back({i, j, 0});
            pos[5].push_back({i, j, n-1});
        }
    }

    vector<tuple<int, int, int>> check;
    rep(i, n) rep(j, n) rep(k, n) {
        int x = i == 0 || i == n-1;
        int y = j == 0 || j == n-1;
        int z = k == 0 || k == n-1;
        if (x+y+z >= 2) {
            check.push_back({i, j, k});
        }
    }

    auto dfs = [&] (auto &&dfs, int idx, int seen) -> bool {
        // idx番目の正方形を割り当てる

        if (idx == 6) {
            for (const auto &[x, y, z] : check) {
                if (G[x][y][z] != 1) {
                    return false;
                }
            }
            return true;
        }

        for (int i = 1; i < 6; ++i) if ((seen >> i & 1) == 0) {
            // 面iに割り当てる
            int nxt = seen | (1 << i);
            rep(j, 4) {
                rotate(X[idx]);
                rep(k, 2) {
                    flip(X[idx]);

                    bool ok = true;
                    for (const auto &[x, y, z] : pos[i]) {
                        if (i == 0 || i == 1) G[x][y][z] += X[idx][y][z];
                        else if (i == 2 || i == 3) G[x][y][z] += X[idx][x][z];
                        else if (i == 4 || i == 5) G[x][y][z] += X[idx][x][y];
                        ok &= G[x][y][z] <= 1;
                    }

                    if (ok && dfs(dfs, idx+1, nxt)) {
                        return true;
                    }

                    for (const auto &[x, y, z] : pos[i]) {
                        if (i == 0 || i == 1) G[x][y][z] -= X[idx][y][z];
                        else if (i == 2 || i == 3) G[x][y][z] -= X[idx][x][z];
                        else if (i == 4 || i == 5) G[x][y][z] -= X[idx][x][y];
                    }
                }
            }
        }

        return false;
    };

    for (const auto &[x, y, z] : pos[0]) {
        G[x][y][z] += X[0][y][z];
    }

    if (dfs(dfs, 1, 1)) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }

    return true;
}


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
    cerr << fixed << setprecision(15);
    while (true) {
        if (!solve()) {
            break;
        }
    }
}
accuracy: 1
time_ms: 60
memory_kb: 3712
final_rank: 18

code_snippet: // #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;

// #include "titan_cpplib/others/print.cpp"

bool solve() {
    int n;
    cin >> n;
    if (n == 0) return false;

    using S = vector<vector<int>>;
    vector<S> X(6);
    rep(i, 6) {
        S x(n, vector<int>(n, 0));
        rep(j, n) {
            string s; cin >> s;
            rep(k, n) {
                x[j][k] = s[k] == 'X' ? 1 : 0;
            }
        }
        X[i] = x;
    }

    vector<vector<vector<int>>> G(n, vector<vector<int>>(n, vector<int>(n, 0)));

    auto rotate = [&] (S &R) -> void {
        auto P = R;
        rep(i, n) R[i][n-1] = P[0][i];
        rep(i, n) R[n-1][n-1-i] = P[i][n-1];
        rep(i, n) R[i][0] = P[n-1][i];
        rep(i, n) R[0][n-1-i] = P[i][0];
    };

    auto flip = [&] (S &R) -> void {
        rep(j, n/2) swap(R[0][j], R[0][n-1-j]);
        rep(j, n/2) swap(R[n-1][j], R[n-1][n-1-j]);
        for (int i = 1; i < n-1; ++i) swap(R[i][0], R[i][n-1]);
    };

    vector<vector<tuple<int, int, int>>> pos(6);
    rep(i, n) rep(j, n) {
        if (i == 0 || j == 0 || i == n-1 || j == n-1) {
            pos[0].push_back({0, i, j});
            pos[1].push_back({n-1, i, j});
            pos[2].push_back({i, 0, j});
            pos[3].push_back({i, n-1, j});
            pos[4].push_back({i, j, 0});
            pos[5].push_back({i, j, n-1});
        }
    }

    vector<tuple<int, int, int>> check;
    rep(i, n) rep(j, n) rep(k, n) {
        int x = i == 0 || i == n-1;
        int y = j == 0 || j == n-1;
        int z = k == 0 || k == n-1;
        if (x+y+z >= 2) {
            check.push_back({i, j, k});
        }
    }

    auto dfs = [&] (auto &&dfs, int idx, int seen) -> bool {
        // idx番目の正方形を割り当てる

        if (idx == 6) {
            for (auto [x, y, z] : check) {
                if (G[x][y][z] != 1) {
                    return false;
                }
            }
            return true;
        }

        for (int i = 1; i < 6; ++i) if ((seen >> i & 1) == 0) {
            // 面iに割り当てる
            int nxt = seen | (1 << i);
            rep(j, 4) {
                rotate(X[idx]);
                rep(k, 2) {
                    flip(X[idx]);

                    bool ok = true;
                    for (auto [x, y, z] : pos[i]) {
                        if (i == 0 || i == 1) G[x][y][z] += X[idx][y][z];
                        else if (i == 2 || i == 3) G[x][y][z] += X[idx][x][z];
                        else if (i == 4 || i == 5) G[x][y][z] += X[idx][x][y];
                        ok &= G[x][y][z] <= 1;
                    }

                    if (ok && dfs(dfs, idx+1, nxt)) {
                        return true;
                    }

                    for (auto [x, y, z] : pos[i]) {
                        if (i == 0 || i == 1) G[x][y][z] -= X[idx][y][z];
                        else if (i == 2 || i == 3) G[x][y][z] -= X[idx][x][z];
                        else if (i == 4 || i == 5) G[x][y][z] -= X[idx][x][y];
                    }
                }
            }
        }

        return false;
    };

    for (auto [x, y, z] : pos[0]) {
        G[x][y][z] += X[0][y][z];
    }

    if (dfs(dfs, 1, 1)) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }

    return true;
}


int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
    cerr << fixed << setprecision(15);
    while (true) {
        if (!solve()) {
            break;
        }
    }
}
accuracy: 1
time_ms: 740
memory_kb: 3584
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
bool solve();

using ll = long long;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
typedef pair<int, int> pii;
typedef vector<int> vi;
template <class T>
inline bool chmin(T &a, T b) {
  return ((a > b) ? (a = b, 1) : (0));
}
template <class T>
inline bool chmax(T &a, T b) {
  return ((a < b) ? (a = b, 1) : (0));
}

int main() {
  while (solve());
  return 0;
}

void cw(vector<string>&s){
  auto t=s;
  rep(i,0,sz(s)){
    rep(j,0,sz(s[0])){
      s[i][j]=t[sz(s)-1-j][i]; 
    }
  }
}

bool solve() {
  int n;cin>>n;
  if (n==0) return 0;

  vector S(6,vector<string>(n,""));
  rep(i,0,6){
    rep(j,0,n)cin>>S[i][j];
  }
  vector cnt(n,vector(n,vi(n,0)));
  //3次元配列にピース数を足す
  auto apply=[&](const vector<string>&s,int bx,int by,int bz,int d,int c){
    bool f=0;
    int x,y,z;
    x=y=z=0;
    //d 0 xbase 1 ybase 2 zbase
    rep(i,0,n){
      if(d==0){x=bx,y=i, z=0;}
      if(d==1){x=0, y=by,z=i;}
      if(d==2){x=0, y=i, z=bz;}
      if(s[i][0]=='X'){
        cnt[x][y][z]+=c;
        if(cnt[x][y][z]>1)f=1;
      }
      if(d==0){z=n-1;}
      if(d==1){x=n-1;}
      if(d==2){x=n-1;}
      if(s[i][n-1]=='X'){
        cnt[x][y][z]+=c;
        if(cnt[x][y][z]>1)f=1;
      }      
    }
    rep(j,1,n-1){
      if(d==0){x=bx,y=0, z=j;}
      if(d==1){x=j, y=by,z=0;}
      if(d==2){x=j, y=0, z=bz;}
      if(s[0][j]=='X'){
        cnt[x][y][z]+=c;
        if(cnt[x][y][z]>1)f=1;
      }
      if(d==0){y=n-1;}
      if(d==1){z=n-1;}
      if(d==2){y=n-1;}
      if(s[n-1][j]=='X'){
        cnt[x][y][z]+=c;
        if(cnt[x][y][z]>1)f=1;
      }
    }
    return f;
  };
  vi used(6);
  apply(S[0],0,-1,-1,0,1);
  used[0]=1;
 
  auto dfs=[&](auto dfs,int i){
    if(i==6) {
      int csum=0;
      rep(i,0,n)rep(j,0,n)rep(k,0,n){
        csum+=cnt[i][j][k];
      }
      if(csum==n*n*n-(n-2)*(n-2)*(n-2)-6*(n-2)*(n-2))return 1;
      return 0;
    }
    int bx,by,bz,d;
    bx=by=bz=d=0;
    if(i==1)bx=n-1,d=0;
    if(i==2)by=0,d=1;
    if(i==3)by=n-1,d=1;
    if(i==4)bz=0,d=2;
    if(i==5)bz=n-1,d=2;
    rep(j,1,6){
      auto T=S[j];
      if(!used[j]){
        used[j]=1;
        rep(k,0,2){
          rep(l,0,4){
            if(!apply(T,bx,by,bz,d,1)){
              if(dfs(dfs,i+1))return 1;
            }
            apply(T,bx,by,bz,d,-1);
            //回転
            cw(T);
          }
          
          // //裏返す
          reverse(all(T));
        }
        used[j]=0;
      }
    }
    return 0;
  };
  if(dfs(dfs,1)){
    cout<<"Yes"<<endl;
  }else{
    cout<<"No"<<endl;
  }
  return 1;
}
accuracy: 1
time_ms: 990
memory_kb: 3456
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__)
#define INF 1001001001001001001ll
#define inf (int)1001001000

ll gcd(ll x, ll y) {	if (x == 0) return y;	return gcd(y%x, x);} 
ll lcm(ll x, ll y) { __int128_t xx,yy; xx=x; yy=y; __int128_t ans=xx * yy / gcd(x, y); ll ans2=ans; return ans2; }
template<typename T>
T POW(T x, ll n){T ret=1;	while(n>0){		if(n&1) ret=ret*x;		x=x*x;		n>>=1;	}	return ret;}
template<typename T>
T modpow(T a, ll n, T p) {	if(n==0) return (T)1;  if (n == 1) return a % p;  if (n % 2 == 1) return (a * modpow(a, n - 1, p)) % p;  T t = modpow(a, n / 2, p);  return (t * t) % p;}
template<typename T>
T modinv(T a, T m) {	if(m==0)return (T)1;	T b = m, u = 1, v = 0;	while (b) {		T t = a / b;		a -= t * b; swap(a, b);		u -= t * v; swap(u, v);	}	u %= m;	if (u < 0) u += m;	return u;}
/*
const int MAXCOMB=510000;
std::vector<mint> fac(MAXCOMB), finv(MAXCOMB), inv(MAXCOMB);
void COMinit() {fac[0] = fac[1] = 1;finv[0] = finv[1] = 1;inv[1] = 1;for (int i = 2; i < MAXCOMB; i++) {fac[i] = fac[i - 1] * i;inv[i] = mint(0) - inv[mint::mod() % i] * (mint::mod() / i);finv[i] = finv[i - 1] * inv[i];}}
mint COM(int n, int k) {if (n < k) return 0;if (n < 0 || k < 0) return 0;return fac[n] * finv[k] * finv[n - k];}
*/
template <typename T> inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false));}
template <typename T> inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false));}

int SOLVEFIN = 0;

void solve(){
    int n;
    cin>>n;
    if(n==0){
        SOLVEFIN=1;
        return;
    }
    vector<vector<vector<string>>> grid(6,vector<vector<string>>(8,vector<string>(n)));
    rep(i,6){
        rep(j,n){
            cin>>grid[i][0][j];
        }
    }
    vector<vector<vector<int>>> cube(n,vector<vector<int>>(n,vector<int>(n,0)));
    rep(i,6){
        rep(j,3){
            rep(k,n){
                rep(l,n){
                    grid[i][j+1][k][l]=grid[i][j][l][n-1-k];
                }
            }
        }
    }
    rep(i,6){
        rep(k,n){
            rep(l,n){
                grid[i][4][k][l]=grid[i][0][k][n-1-l];
            }
        }
    }
    rep(i,6){
        rep(j,3){
            rep(k,n){
                rep(l,n){
                    grid[i][j+5][k][l]=grid[i][j+4][l][n-1-k];
                }
            }
        }
    }
    int cnt = 0;
    rep(i,6){
        rep(j,n){
            rep(k,n){
                //cout<<grid[i][0][j][k];
                if((j==0 || j==n-1 || k==0 || k==n-1) && grid[i][0][j][k]=='X')cnt++;
            }
            //cout<<endl;
        }
    }
    //cout<<cnt<<endl;
    if(cnt!=n*n*n-(n-2)*(n-2)*(n-2)-(n-2)*(n-2)*6){
        cout<<"No"<<endl;
        return;
    }

    vector<pair<int,int>> indpr;
    rep(i,n){
        rep(j,n){
            if((i==0 || i==n-1 )||(j==0 || j==n-1))indpr.push_back({i,j});
        }
    }

    int pos = 0;
    int maxdir = 0;
    int key = 1;
    vector<int> ord(n); //組み上げる順番

    auto dfs = [&](auto &&dfs, int dir) -> void{
        if(dir==6){
           pos = 1;
           return;
        }
        if(pos)return;
        // if(dir==5 && key){
        //     rep(i,n){
        //         rep(j,n){
        //             rep(k,n){
        //                 cout<<cube[i][j][k];
        //             }
        //             cout<<endl;
        //         }
        //         cout<<endl;
        //     }
        //     key=0;
        //     cout<<ord[dir]<<endl;
        // }

        int nowgrid = ord[dir];
        for(int j=0;j<8;j++){
            // if(dir==0 && (j!=0 && j!=4))continue;
            vector<vector<int>> allcor;
            int nowpos = 1;
            for(auto pr : indpr){
                if(grid[nowgrid][j][pr.first][pr.second]=='.')continue;
                vector<int> cor(3,0);
                if(dir==0){
                    cor[0]=pr.first;
                    cor[1]=pr.second;
                }
                else if(dir==1){
                    cor[0] = n-1;
                    cor[1]=pr.first;
                    cor[2]=pr.second;
                }
                else if(dir==2){
                    cor[1] = n-1;
                    cor[0]=pr.first;
                    cor[2]=pr.second;
                }
                else if(dir==3){
                    cor[1]=pr.first;
                    cor[2]=pr.second;
                }
                else if(dir==4){
                    cor[0]=pr.first;
                    cor[2]=pr.second;
                }else{  
                    cor[2] = n-1;
                    cor[0]=pr.first;
                    cor[1]=pr.second;
                }
                allcor.push_back(cor);
                if(cube[cor[0]][cor[1]][cor[2]]){
                    nowpos = 0;
                    break;
                }
            }
            if(nowpos ==0)continue;
            for(vector<int> cor:allcor){
                cube[cor[0]][cor[1]][cor[2]]=dir+1;
            }
            dfs(dfs,dir+1);
            for(vector<int> cor:allcor){
                cube[cor[0]][cor[1]][cor[2]]=0;
            }
        }
    };

    ord = {0,1,2,3,4,5};
    dfs(dfs,0);
    ord = {0,1,3,2,4,5};
    dfs(dfs,0);
    ord = {0,1,3,4,2,5};
    dfs(dfs,0);

    ord = {0,1,2,3,5,4};
    dfs(dfs,0);
    ord = {0,1,3,2,5,4};
    dfs(dfs,0);
    ord = {0,1,2,5,3,4};
    dfs(dfs,0);

    ord = {0,1,2,4,5,3};
    dfs(dfs,0);
    ord = {0,1,4,2,5,3};
    dfs(dfs,0);
    ord = {0,1,2,5,4,3};
    dfs(dfs,0);

    ord = {0,1,3,4,5,2};
    dfs(dfs,0);
    ord = {0,1,3,5,4,2};
    dfs(dfs,0);
    ord = {0,1,5,3,4,2};
    dfs(dfs,0);

    ord = {0,5,2,3,4,1};
    dfs(dfs,0);
    ord = {0,5,3,2,4,1};
    dfs(dfs,0);
    ord = {0,5,2,4,3,1};
    dfs(dfs,0);
    //cout<<maxdir<<endl;



    cout<< (pos? "Yes" : "No")<<endl;

}

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	while(SOLVEFIN == 0) solve();
}
accuracy: 1
time_ms: 230
memory_kb: 3328
final_rank: 2

code_snippet: #include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
typedef int ll;
typedef unsigned long long ull;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rrep(i,start,end) for (ll i = start;i >= (ll)(end);i--)
#define repn(i,end) for(ll i = 0; i <= (ll)(end); i++)
#define reps(i,start,end) for(ll i = start; i < (ll)(end); i++)
#define repsn(i,start,end) for(ll i = start; i <= (ll)(end); i++)
#define each(p,a) for(auto &p:a)
typedef vector<ll> vll;
typedef vector<pair<ll ,ll>> vpll;
typedef vector<vector<ll>> vvll;
typedef set<ll> sll;
typedef map<ll , ll> mpll;
typedef pair<ll ,ll> pll;
typedef tuple<ll , ll , ll> tpl3;
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__; input(__VA_ARGS__)
#define Str(...) string __VA_ARGS__; input(__VA_ARGS__)
#define Ch(...) char __VA_ARGS__; input(__VA_ARGS__)
#define all(a)  (a).begin(),(a).end()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define sz(x) (ll)x.size()
// << std::fixed << std::setprecision(10)
// const ll INF = 1LL << 60;
const ld EPS = 1e-9;
 
inline ll lfloor(ll x,ll m){return (x - ((x % m+ m)%m))/m;}
inline ll positive_mod(ll a,ll m){return (a % m + m)%m;}
inline ll popcnt(ull a){ return __builtin_popcountll(a);}
//0indexed
inline ll topbit(ll a){assert(a != 0);return 63 - __builtin_clzll(a);}
inline ll smlbit(ll a){assert(a != 0);return __builtin_ctzll(a);}
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;}
template<typename T> std::istream &operator>>(std::istream&is,std::vector<T>&v){for(T &in:v){is>>in;}return is;}
template<typename T> std::ostream &operator<<(std::ostream&os,const std::vector<T>&v){for(auto it=std::begin(v);it!=std::end(v);){os<<*it<<((++it)!=std::end(v)?" ":"");}return os;}
template<typename T1, typename T2>std::ostream &operator<< (std::ostream &os, std::pair<T1,T2> p){os << "{" << p.first << "," << p.second << "}";return os;}
template<class... T>void input(T&... a){(cin >> ... >> a);}
void print(){cout << endl;}
template<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a;((cout << ' ' << b), ...);cout << endl;}
template<class T> void pspace(const T& a){ cout << a << ' ';}
void perr(){cerr << endl;}
template<class T, class... Ts>void perr(const T& a, const Ts&... b){cerr << a;((cerr << ' ' << b), ...);cerr << endl;}
void yes(bool i = true){ return print(i?"yes":"no"); }
void Yes(bool i = true){ return print(i?"Yes":"No"); }
void YES(bool i = true){ return print(i?"YES":"NO"); }
template <class T> vector<T> &operator++(vector<T> &v) {for(auto &e : v) e++;return v;}
template <class T> vector<T> operator++(vector<T> &v, signed) {auto res = v;for(auto &e : v) e++;return res;}
template <class T> vector<T> &operator--(vector<T> &v) {for(auto &e : v) e--;return v;}
template <class T> vector<T> operator--(vector<T> &v, signed) {auto res = v;for(auto &e : v) e--;return res;}
//grid探索用
vector<ll> _ta = {0,0,1,-1,1,1,-1,-1};
vector<ll> _yo = {1,-1,0,0,1,-1,1,-1};
bool isin(ll now_i,ll now_j,ll h,ll w){return (0<=now_i && now_i < h && 0 <= now_j && now_j < w);}
	
ll lpow(ll x,ll n){ll ans = 1;while(n >0){if(n & 1)ans *= x;x *= x;n >>= 1;}return ans;}
ll Modlpow(ll x,ll n,ll m){ll ans = 1;ll a = x%m;while(n >0){if(n & 1){ans *= a;ans%= m;}a *= a;a %= m;n >>= 1;}return ans;} 
const ll MOD9 = 998244353LL;
const ll MOD10 = 1000000007LL;

void solve(ll n){
	vector<vvll> g(6,vvll(n,vll(n)));
	rep(i,6){
		rep(j,n){
			Str(s);
			rep(k,n)if(s[k] == 'X')g[i][j][k] = 1;
		}
	}
	
	vector ng(6,vector(4,vector(2,vector(n,vector<ll>(n)))));
	
	auto rot = [&](vvll &now){
		vvll ret(n,vll(n));
		rep(i,n){
			rep(j,n){
				ret[j][n-1-i] = now[i][j];
			}
		}
		return ret;
	};
	auto rev = [&](vvll &now){
		vvll ret(n,vll(n));
		rep(i,n){
			rep(j,n){
				ret[i][n-1-j] = now[i][j];
			}
		}
		return ret;
	};
	rep(i,6){
		vvll now = g[i];
		rep(j,4){
			ng[i][j][0] = now;
			now = rot(now);
		}
		now = rev(now);
		rep(j,4){
			ng[i][j][1] = now;
			now = rot(now);
		}
	}

	// //dbg
	// rep(i,4){
	// 	rep(j,2){
	// 		print(i,j);
	// 		rep(k,n){
	// 			rep(l,n){
	// 				cout << ng[0][i][j][k][l];
	// 			}
	// 			cout << endl;
	// 		}
	// 	}
	// }
	
	vll ord(5);
	iota(all(ord),0);
	//固定面
	
	bool answer = false;


	//面情報

	vector mid(6,vpll(4));

	mid[0] = {{0,1},{1,5},{4,5},{0,4}};
	mid[1] = {{3,2},{2,1},{0,1},{3,0}};
	mid[2] = {{1,2},{2,6},{5,6},{1,5}};
	mid[3] = {{6,7},{7,4},{5,4},{6,5}};
	mid[4] = {{3,0},{0,4},{7,4},{3,7}};
	mid[5] = {{2,3},{3,7},{6,7},{2,6}};
	

	map<pll,vll> mp;
	rep(i,6){
		for(auto [u,v]:mid[i]){
			mp[{min(u,v),max(u,v)}] = vll(n);
		}
	}
	vll corner(8);
		
	ll checknum = 0;

	vll rotstate(6);

	vll revstate(6);

	auto check = [&](){
		// cerr << rotstate << endl;
		// cerr << revstate << endl;
		// cerr << ord << endl;
		checknum++;
		each(p,mp){
			reps(i,1,n-1){
				if(p.second[i] == 0)return ;
			}
		}
		each(p,corner){
			if(p == 0)return ;
		}
		answer = true;
	};

	auto puts = [&](ll sid, ll id,ll ro ,ll re) -> bool {
		//置く
		//おけないなら戻す 
		//おけたらtrue

		// おけるか
		vll l(4),r(4),rv(4);
		rep(i,4){
			l[i] = mid[sid][i].first;
			r[i] = mid[sid][i].second;
			rv[i] = l[i] < r[i];
		}
		vpll pr(4);
		rep(i,4){
			pr[i] ={min(l[i],r[i]),max(l[i],r[i])};
		}

		reps(i,1,n-1){
			//上
			if(ng[id][ro][re][0][i] && mp[pr[0]][rv[0]?i:n-1-i]) return false;
			//右
			if(ng[id][ro][re][i][n-1] && mp[pr[1]][rv[1]?i:n-1-i]) return false;
			//下
			if(ng[id][ro][re][n-1][i] && mp[pr[2]][rv[2]?i:n-1-i])return false;
			//左
			if(ng[id][ro][re][i][0] && mp[pr[3]][rv[3]?i:n-1-i])return false;
		}
		//角
		if(ng[id][ro][re][0][0] && corner[l[0]])return false;//左上
		if(ng[id][ro][re][0][n-1] && corner[l[1]])return false;//右上
		if(ng[id][ro][re][n-1][n-1] && corner[r[2]])return false;//右下
		if(ng[id][ro][re][n-1][0] && corner[r[3]])return false;//左下
		//置く
		reps(i,1,n-1){
			//上
			if(ng[id][ro][re][0][i]) mp[pr[0]][rv[0]?i:n-1-i] = 1;
			//右
			if(ng[id][ro][re][i][n-1]) mp[pr[1]][rv[1]?i:n-1-i] = 1;
			//下
			if(ng[id][ro][re][n-1][i]) mp[pr[2]][rv[2]?i:n-1-i] = 1;
			//左
			if(ng[id][ro][re][i][0]) mp[pr[3]][rv[3]?i:n-1-i] = 1;
		}
		if(ng[id][ro][re][0][0])corner[l[0]] = 1;//左上
		if(ng[id][ro][re][0][n-1])corner[l[1]] = 1;//右上
		if(ng[id][ro][re][n-1][n-1])corner[r[2]] = 1;//右下
		if(ng[id][ro][re][n-1][0])corner[r[3]] = 1;//左下

		return true;
	};

	auto erase = [&](ll sid,ll id ,ll ro,ll re){
		//消す
		//置けている前提
		//消す
		// おけるか
		vll l(4),r(4),rv(4);
		rep(i,4){
			l[i] = mid[sid][i].first;
			r[i] = mid[sid][i].second;
			rv[i] = l[i] < r[i];
		}
		vpll pr(4);
		rep(i,4){
			pr[i] ={min(l[i],r[i]),max(l[i],r[i])};
		}
		//消す
		reps(i,1,n-1){
			//上
			if(ng[id][ro][re][0][i]){assert(mp[pr[0]][rv[0]?i:n-1-i]);mp[pr[0]][rv[0]?i:n-1-i] = 0;} 
			//右
			if(ng[id][ro][re][i][n-1]){ assert(mp[pr[1]][rv[1]?i:n-1-i]); mp[pr[1]][rv[1]?i:n-1-i] = 0;}
			//下
			if(ng[id][ro][re][n-1][i]){assert(mp[pr[2]][rv[2]?i:n-1-i]); mp[pr[2]][rv[2]?i:n-1-i] = 0;}
			//左
			if(ng[id][ro][re][i][0]){assert(mp[pr[3]][rv[3]?i:n-1-i]); mp[pr[3]][rv[3]?i:n-1-i] = 0;}
		}
		if(ng[id][ro][re][0][0]){assert(corner[l[0]]); corner[l[0]] = 0;}//左上
		if(ng[id][ro][re][0][n-1]){assert(corner[l[1]]);corner[l[1]] = 0;}//右上
		if(ng[id][ro][re][n-1][n-1]){assert(corner[r[2]]); corner[r[2]] = 0;}//右下
		if(ng[id][ro][re][n-1][0]){assert(corner[r[3]]); corner[r[3]] = 0;}//左下
	};


	auto dfs = [&](auto dfs, ll v)->void{
		if(answer) return ;
		if(v == 4){
			// cerr << "here" << endl;
			rep(i,4){
				rep(j,2){
					rotstate[ord[v]] = i;
					revstate[ord[v]] = j;
					//置く
					bool ret = puts(v,ord[v],i,j);
					if(!ret)continue;
					//判定
					check();
					//外す
					erase(v,ord[v],i,j);
				}
			}
			return ;
		}
		rep(i,4){
			rep(j,2){
				rotstate[ord[v]] = i;
				revstate[ord[v]] = j;
				//置く
				bool ret = puts(v,ord[v],i,j);
				if(!ret){continue;}
				//次
				dfs(dfs,v+1);
				//外す
				erase(v,ord[v],i,j);
			}
		}
	};
	
	do{
		if(answer)break;
		rep(z,2){		
			revstate[5] = z;
			//置く
			bool ret = puts(5,5,0,z);
			if(!ret)continue;
			dfs(dfs,0);
			//消す
			erase(5,5,0,z);
		}
	}while(next_permutation(all(ord)));

	// cerr << checknum << endl;

	Yes(answer);
	
}
 
int main(){
	ios::sync_with_stdio(false);cin.tie(nullptr);
	while(true){
		LL(n);
		if(n == 0)break;
		solve(n);
	}

}
accuracy: 1
time_ms: 2760
memory_kb: 3712
final_rank: 20

code_snippet: #include <bits/extc++.h>

using ll = long long;

using pii = std::pair<int, int>;

using tiii = std::tuple<int, int, int>;

template <typename T>
bool chmax(T& x, const T& v) {
  if (x < v) {
    x = v;
    return true;
  } else {
    return false;
  }
}

bool solve() {
  int N;
  std::cin >> N;
  if (N == 0) {
    return true;
  }
  std::array<std::vector<std::string>, 6> X;
  for (auto& xs : X) {
    xs = std::vector<std::string>(N);
    for (auto& x : xs) {
      std::cin >> x;
    }
  }

  auto rot = [&](int i, pii p) -> pii {
    while (i--) {
      p = {p.second, N - 1 - p.first};
    }

    return p;
  };

  auto flip = [&](bool f, pii p) -> pii {
    if (f) {
      p = {p.second, p.first};
    }

    return p;
  };

  std::array<int, 6> indices = {0, 1, 2, 3, 4, 5};

  auto map = [&](int i, pii p) -> tiii {
    int t = 0;

    if (i & 1) {
      t = N - 1;
    }

    auto [x, y] = p;

    if ((i >> 1) == 0) {
      return {x, y, t};
    } else if ((i >> 1) == 1) {
      return {y, t, x};
    } else {
      return {t, x, y};
    }
  };

  do {
    if (indices[0] != 0) break;

    std::vector table(N, std::vector(N, std::vector(N, -1)));

    auto rec = [&](auto rec, int i) -> bool {
      if (i == 6) {
        for (int k = 0; k < N; k++) {
          for (int x = 0; x < N; x += N - 1) {
            for (int y = 0; y < N; y += N - 1) {
              if (table[k][x][y] == -1) {
                return false;
              }
              if (table[y][k][x] == -1) {
                return false;
              }
              if (table[x][y][k] == -1) {
                return false;
              }
            }
          }
        }

        return true;
      }

      for (int s = 0; s < (i > 0 ? 8 : 1); s++) {
        bool isok = true;

        for (int aaa = 0; aaa < N - 1; aaa++) {
          for (int bbb = 0; bbb < 4; bbb++) {
            auto [x, y] = rot(bbb, {aaa, 0});

            if (X[indices[i]][x][y] == '.') continue;

            auto [a, b, c] = map(i, flip(s & 1, rot(s >> 1, {x, y})));

            if (table[a][b][c] >= 0) {
              isok = false;
              goto escape;
            }

            table[a][b][c] = i;
          }
        }

        if (isok) {
          if (rec(rec, i + 1)) {
            return true;
          }
        }

      escape:

        for (int k = 0; k < N; k++) {
          for (int x = 0; x < N; x += N - 1) {
            for (int y = 0; y < N; y += N - 1) {
              if (table[k][x][y] == i) table[k][x][y] = -1;
              if (table[x][k][y] == i) table[x][k][y] = -1;
              if (table[y][x][k] == i) table[y][x][k] = -1;
            }
          }
        }
      }

      return false;
    };

    if (rec(rec, 0)) {
      std::cout << "Yes\n";
      return false;
    }

    for (int s0 = 0; s0 < 0; s0++) {
      for (int s1 = 0; s1 < 8; s1++) {
        for (int s2 = 0; s2 < 8; s2++) {
          for (int s3 = 0; s3 < 8; s3++) {
            for (int s4 = 0; s4 < 8; s4++) {
              for (int s5 = 0; s5 < 8; s5++) {
                std::array<int, 6> state = {s0, s1, s2, s3, s4, s5};

                bool isok = true;

                for (int i = 0; i < 6; i++) {
                  for (int aaa = 0; aaa < N - 1; aaa++) {
                    for (int bbb = 0; bbb < 4; bbb++) {
                      auto [x, y] = rot(bbb, {aaa, 0});

                      if (X[indices[i]][x][y] == '.') continue;

                      auto [a, b, c] = map(
                          i, flip(state[i] & 1, rot(state[i] >> 1, {x, y})));

                      if (table[a][b][c]) {
                        isok = false;
                        goto escape;
                      }

                      table[a][b][c] = true;
                    }
                  }
                }

                for (int i = 0; i < N; i++) {
                  for (int x = 0; x < N; x += N - 1) {
                    for (int y = 0; y < N; y += N - 1) {
                      if (!table[i][x][y]) {
                        isok = false;
                        goto escape;
                      }
                      if (!table[y][i][x]) {
                        isok = false;
                        goto escape;
                      }
                      if (!table[x][y][i]) {
                        isok = false;
                        goto escape;
                      }
                    }
                  }
                }

              escape:

                if (isok) {
                  std::cout << "Yes\n";
                  return false;
                }

                for (int i = 0; i < N; i++) {
                  for (int x = 0; x < N; x += N - 1) {
                    for (int y = 0; y < N; y += N - 1) {
                      table[i][x][y] = table[y][i][x] = table[x][y][i] = false;
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

  } while (std::next_permutation(indices.begin(), indices.end()));

  std::cout << "No\n";

  return false;
}

int main() {
  while (!solve());
}
accuracy: 1
time_ms: 640
memory_kb: 3456
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define rep(i,l,r) for(int i=(l);i<(r);i++)
#define all(v) begin(v),end(v)
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
int main(){
  int N;
  while(1){
    cin>>N;
    if(N==0)return 0;
    using edge= array<int,4>;
    auto rotate=[&](edge a) -> edge {
      return edge{a[1],a[2],a[3],a[0]};
    };
    auto reverseEdge=[&](int a) -> int {
      int ret=0;
      rep(i,0,N)ret|=(a>>i&1)<<(N-1-i);
      return ret;
    };
    auto flip=[&](edge a) -> edge {
      edge ret{0,0,0,0};
      ret[0]=reverseEdge(a[0]);
      ret[1]=reverseEdge(a[3]);
      ret[2]=reverseEdge(a[2]);
      ret[3]=reverseEdge(a[1]);
      return ret;
    };

    auto edgeCover=[&](int a,int b) -> bool {
      int c=((1<<(N-1))-2);
      if(a&b)return false;
      if((c&(a|b))!=c)return false;
      return true;
    };
    vector<edge> A(6);
    rep(i,0,6){
      vector<string> S(N);
      rep(j,0,N){
        cin>>S[j];
      }
      edge e{0,0,0,0};
      rep(i,0,N){
        if(S[0][i]=='X')e[0]|=1<<(N-1-i);
        if(S[i][N-1]=='X')e[1]|=1<<(N-1-i);
        if(S[N-1][i]=='X')e[2]|=1<<i;
        if(S[i][0]=='X')e[3]|=1<<i;
      }
      A[i]=e;
    }

    auto output=[&](int v){
      rep(i,0,N)cerr<<(v>>(N-1-i)&1);
      cerr<<'/';
    };
    // cerr<<edgeCover(A[0][0],reverseEdge(A[1][0]))<<endl;
    // output(A[0][0]);
    // output(A[0][1]);
    // output(A[0][2]);
    // output(A[0][3]);
    // cerr<<endl;
    // output(rotate(A[0])[0]);
    // output(rotate(A[0])[1]);
    // output(rotate(A[0])[2]);
    // output(rotate(A[0])[3]);
    // cerr<<endl;
    vi P(6);
    iota(all(P),0);
    bool ok=0;
    vector<edge> v;
    auto dfs=[&](auto f,int n) -> bool {
      if(n==6){
        bool now=1;
        if((v[0][0]&1)+(v[1][1]&1)+(v[3][3]&1)!=1)now=0;
        if((v[1][0]&1)+(v[3][0]&1)+(v[4][3]&1)!=1)now=0;
        if((v[1][3]&1)+(v[4][0]&1)+(v[5][3]&1)!=1)now=0;
        if((v[5][0]&1)+(v[1][2]&1)+(v[0][3]&1)!=1)now=0;

        if((v[2][0]&1)+(v[0][1]&1)+(v[3][2]&1)!=1)now=0;
        if((v[2][1]&1)+(v[3][1]&1)+(v[4][2]&1)!=1)now=0;
        if((v[2][2]&1)+(v[4][1]&1)+(v[5][2]&1)!=1)now=0;
        if((v[2][3]&1)+(v[5][1]&1)+(v[0][2]&1)!=1)now=0;
        
        if(!edgeCover(v[0][0],reverseEdge(v[1][2])))now=0;
        if(!edgeCover(v[0][2],reverseEdge(v[2][0])))now=0;
        
        if(!edgeCover(v[0][1],reverseEdge(v[3][3])))now=0;
        if(!edgeCover(v[3][0],reverseEdge(v[1][1])))now=0;
        if(!edgeCover(v[3][2],reverseEdge(v[2][1])))now=0;
        
        if(!edgeCover(v[3][1],reverseEdge(v[4][3])))now=0;
        if(!edgeCover(v[4][0],reverseEdge(v[1][0])))now=0;
        if(!edgeCover(v[4][2],reverseEdge(v[2][2])))now=0;
        
        if(!edgeCover(v[4][1],reverseEdge(v[5][3])))now=0;
        if(!edgeCover(v[5][0],reverseEdge(v[1][3])))now=0;
        if(!edgeCover(v[5][1],reverseEdge(v[0][3])))now=0;
        if(!edgeCover(v[5][2],reverseEdge(v[2][3])))now=0;
        return now;
      }
      bool ret=0;
      rep(x,0,8){
        edge e=A[P[n]];
        if(x&4)e=flip(e);
        rep(i,0,x%4)e=rotate(e);
        v.push_back(e);
        bool now=1;
        if(n==1)if(!edgeCover(v[0][0],reverseEdge(v[1][2])))now=0;
        if(n==2)if(!edgeCover(v[0][2],reverseEdge(v[2][0])))now=0;

        if(n==3){
          if(!edgeCover(v[0][1],reverseEdge(v[3][3])))now=0;
          if(!edgeCover(v[3][0],reverseEdge(v[1][1])))now=0;
          if(!edgeCover(v[3][2],reverseEdge(v[2][1])))now=0;
        }
        
        if(n==4){
          if(!edgeCover(v[3][1],reverseEdge(v[4][3])))now=0;
          if(!edgeCover(v[4][0],reverseEdge(v[1][0])))now=0;
          if(!edgeCover(v[4][2],reverseEdge(v[2][2])))now=0;
        }
        if(n==5){
          if(!edgeCover(v[4][1],reverseEdge(v[5][3])))now=0;
          if(!edgeCover(v[5][0],reverseEdge(v[1][3])))now=0;
          if(!edgeCover(v[5][1],reverseEdge(v[0][3])))now=0;
          if(!edgeCover(v[5][2],reverseEdge(v[2][3])))now=0;
        }

        if(now&&f(f,n+1))ret=1;
        v.pop_back();
      }
      return ret;
    };
    v.push_back(A[0]);
    do{    
      if(dfs(dfs,1))ok=1;
    }while(next_permutation(P.begin()+1,P.end()));
    cout<<(ok?"Yes\n":"No\n");
  }
}
accuracy: 1
time_ms: 40
memory_kb: 3456
final_rank: 6

code_snippet: #line 2 "/Users/noya2/Desktop/Noya2_library/template/template.hpp"
using namespace std;

#include<bits/stdc++.h>
#line 1 "/Users/noya2/Desktop/Noya2_library/template/inout_old.hpp"
namespace noya2 {

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p){
    os << p.first << " " << p.second;
    return os;
}
template <typename T, typename U>
istream &operator>>(istream &is, pair<T, U> &p){
    is >> p.first >> p.second;
    return is;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v){
    int s = (int)v.size();
    for (int i = 0; i < s; i++) os << (i ? " " : "") << v[i];
    return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v){
    for (auto &x : v) is >> x;
    return is;
}

void in() {}
template <typename T, class... U>
void in(T &t, U &...u){
    cin >> t;
    in(u...);
}

void out() { cout << "\n"; }
template <typename T, class... U, char sep = ' '>
void out(const T &t, const U &...u){
    cout << t;
    if (sizeof...(u)) cout << sep;
    out(u...);
}

template<typename T>
void out(const vector<vector<T>> &vv){
    int s = (int)vv.size();
    for (int i = 0; i < s; i++) out(vv[i]);
}

struct IoSetup {
    IoSetup(){
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
        cerr << fixed << setprecision(7);
    }
} iosetup_noya2;

} // namespace noya2
#line 1 "/Users/noya2/Desktop/Noya2_library/template/const.hpp"
namespace noya2{

const int iinf = 1'000'000'007;
const long long linf = 2'000'000'000'000'000'000LL;
const long long mod998 =  998244353;
const long long mod107 = 1000000007;
const long double pi = 3.14159265358979323;
const vector<int> dx = {0,1,0,-1,1,1,-1,-1};
const vector<int> dy = {1,0,-1,0,1,-1,-1,1};
const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string alp = "abcdefghijklmnopqrstuvwxyz";
const string NUM = "0123456789";

void yes(){ cout << "Yes\n"; }
void no(){ cout << "No\n"; }
void YES(){ cout << "YES\n"; }
void NO(){ cout << "NO\n"; }
void yn(bool t){ t ? yes() : no(); }
void YN(bool t){ t ? YES() : NO(); }

} // namespace noya2
#line 2 "/Users/noya2/Desktop/Noya2_library/template/utils.hpp"

#line 6 "/Users/noya2/Desktop/Noya2_library/template/utils.hpp"

namespace noya2{

unsigned long long inner_binary_gcd(unsigned long long a, unsigned long long b){
    if (a == 0 || b == 0) return a + b;
    int n = __builtin_ctzll(a); a >>= n;
    int m = __builtin_ctzll(b); b >>= m;
    while (a != b) {
        int mm = __builtin_ctzll(a - b);
        bool f = a > b;
        unsigned long long c = f ? a : b;
        b = f ? b : a;
        a = (c - b) >> mm;
    }
    return a << std::min(n, m);
}

template<typename T> T gcd_fast(T a, T b){ return static_cast<T>(inner_binary_gcd(std::abs(a),std::abs(b))); }

long long sqrt_fast(long long n) {
    if (n <= 0) return 0;
    long long x = sqrt(n);
    while ((x + 1) * (x + 1) <= n) x++;
    while (x * x > n) x--;
    return x;
}

template<typename T> T floor_div(const T n, const T d) {
    assert(d != 0);
    return n / d - static_cast<T>((n ^ d) < 0 && n % d != 0);
}

template<typename T> T ceil_div(const T n, const T d) {
    assert(d != 0);
    return n / d + static_cast<T>((n ^ d) >= 0 && n % d != 0);
}

template<typename T> void uniq(std::vector<T> &v){
    std::sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
}

template <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }

template <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }

template<typename T> inline bool range(T l, T x, T r){ return l <= x && x < r; }

} // namespace noya2
#line 8 "/Users/noya2/Desktop/Noya2_library/template/template.hpp"

#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define repp(i,m,n) for (int i = (m); i < (int)(n); i++)
#define reb(i,n) for (int i = (int)(n-1); i >= 0; i--)
#define all(v) (v).begin(),(v).end()

using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pil = pair<int,ll>;
using pli = pair<ll,int>;

namespace noya2{

/*　~ (. _________ . /)　*/

}

using namespace noya2;


#line 2 "c.cpp"

int rev(int n, int x){
    int y = 0;
    rep(i,n){
        y |= ((x >> (n-1-i) & 1) << i);
    }
    return y;
}

struct face {
    int n;
    array<int,4> e;
    void input(int _n){
        n = _n;
        vector<string> a(n); in(a);
        e = {};
        rep(i,n){
            if (a[0][i] == 'X'){
                e[0] |= (1 << i);
            }
        }
        rep(i,n){
            if (a[i][n-1] == 'X'){
                e[1] |= (1 << i);
            }
        }
        rep(i,n){
            if (a[n-1][n-1-i] == 'X'){
                e[2] |= (1 << i);
            }
        }
        rep(i,n){
            if (a[n-1-i][0] == 'X'){
                e[3] |= (1 << i);
            }
        }
    }
    void rot(){
        swap(e[0],e[1]);
        swap(e[1],e[2]);
        swap(e[2],e[3]);
    }
    void flip(){
        rep(i,4){
            e[i] = rev(n,e[i]);
        }
        swap(e[1],e[3]);
    }
};

void solve1(int n){
    vector<face> f(6);
    rep(i,6){
        f[i].input(n);
    }
    int msk = (1<<(n-1))-2;
    auto test = f;
    auto check = [&](int x, int y){
        if ((x&msk)&(y&msk)) return false;
        return ((x&msk)|(y&msk)) == msk;
    };
    auto edge = [&](int i, int di, int j, int dj, bool revd = false){
        if (revd) check(test[i].e[di], test[j].e[dj]);
        return check(test[i].e[di], rev(n,test[j].e[dj]));
    };
    auto vertex = [&](int i, int di, int j, int dj, int k, int dk){
        return (test[i].e[di]&1) + (test[j].e[dj]&1) + (test[k].e[dk]&1) == 1;
    };
    int call = 0;
    auto valid = [&](){
        call++;
        // if (!edge(0,0,4,0)) return false;
        // if (!edge(0,1,3,0)) return false;
        // if (!edge(0,3,5,0)) return false;
        // if (!edge(1,3,5,1)) return false;
        // if (!edge(2,3,5,2)) return false;
        // if (!edge(2,2,4,2)) return false;
        // if (!edge(2,1,3,2)) return false;
        // if (!edge(0,2,1,0,1)) return false;
        // if (!edge(1,2,2,0,1)) return false;
        // if (!edge(1,1,3,3,1)) return false;
        // if (!edge(3,1,4,3,1)) return false;
        // if (!edge(4,1,5,3,1)) return false;
        if (!vertex(0,2,1,1,3,0)) return false;
        if (!vertex(0,1,3,1,4,0)) return false;
        if (!vertex(0,0,4,1,5,0)) return false;
        if (!vertex(1,3,2,0,5,2)) return false;
        if (!vertex(2,3,4,2,5,3)) return false;
        if (!vertex(3,2,4,3,2,2)) return false;
        if (!vertex(0,3,1,0,5,1)) return false;
        if (!vertex(1,2,2,1,3,3)) return false;
        return true;
    };
    auto brunchok = [&](int i){
        if (i == 0) return true;
        if (i == 1){
            if (!edge(0,2,1,0,1)) return false;
            return true;
        }
        if (i == 2){
            if (!edge(1,2,2,0,1)) return false;
            return true;
        }
        if (i == 3){
            if (!edge(0,1,3,0)) return false;
            if (!edge(2,1,3,2)) return false;
            if (!edge(1,1,3,3,1)) return false;
            return true;
        }
        if (i == 4){
            if (!edge(0,0,4,0)) return false;
            if (!edge(2,2,4,2)) return false;
            if (!edge(3,1,4,3,1)) return false;
            return true;
        }
        if (i == 5){
            if (!edge(0,3,5,0)) return false;
            if (!edge(1,3,5,1)) return false;
            if (!edge(2,3,5,2)) return false;
            if (!edge(4,1,5,3,1)) return false;
            return true;
        }
        return false;
    };
    bool ans = false;
    auto dfs = [&](auto sfs, int step) -> void {
        if (ans) return ;
        if (step == 12){
            if (valid()){
                ans = true;
            }
            return ;
        }
        int i = step/2;
        if (step % 2 == 0){
            rep(tt,2){
                sfs(sfs,step+1);
                test[i].flip();
            }
        }
        else {
            rep(tt,4){
                if (brunchok(i)){
                    sfs(sfs,step+1);
                }
                test[i].rot();
            }
        }
    };
    repp(i,1,6){
        int j = (i == 1 ? 2 : 1);
        test[0] = f[0];
        test[1] = f[j];
        test[2] = f[i];
        vector<int> p;
        rep(k,6){
            if (k == 0 || k == i || k == j) continue;
            p.emplace_back(k);
        }
        assert(p.size() == 3u);
        do {
            rep(k,3){
                test[3+k] = f[p[k]];
            }
            dfs(dfs,0);
        }while(next_permutation(all(p)));
    }
    yn(ans);
    // out(call);
}

void solve(){
    int n;
    while (true){
        cin >> n;
        if (n == 0) return ;
        solve1(n);
    }
}

int main(){
    int t = 1; //in(t);
    while (t--) { solve(); }
}
accuracy: 1
time_ms: 50
memory_kb: 3584
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for(ll i = a; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#pragma optimize GCC("Ofast")

int main(){
    while(true) {
        int n;
        cin >> n;
        if(n ==0 )break;
        int sum = 0;

        vector<vector<string>> b(6, vector<string>(n));
        rep(i, 0, 6){
            rep(j, 0, n) {
                cin >> b[i][j];
                rep(k, 0, n) sum += (b[i][j][k]=='X'?1:0);
            }
        }

        if(sum != n*n*2 + (n-2) * (n-1) * 4){
            cout << "No" << endl;
            continue;
        }

        vector<int> iot(5);
        iota(all(iot), 1);

        auto rot = [&](vector<string> f, int p) -> vector<string> {
            if(p >= 4) {
                p -= 4;
                rep(i, 0, n) reverse(all(f[i]));
            }
            rep(i, 0, p) {
                vector<string> nx(n, string(n, '.'));
                rep(j, 0, n){
                    rep(k, 0, n){
                        nx[k][n-1-j] = f[j][k];
                    }
                }
                nx.swap(f);
            }
            return f;
        };
        bool ans = false;

        vector rott(6, vector(8, vector<string>()));
        rep(i, 0, 6){
            rep(j ,0, 8){
                rott[i][j] = rot(b[i], j);
            }
        }

        
        do {
            vector<string> f0 = b[0];
            rep(i1, 0, 8) {
                bool res = true;
                rep(i, 0, n){
                    if(f0[0][i] == 'X' && rott[iot[0]][i1][n-1][i] == 'X') res = false;
                }
                if(!res) continue;
                rep(i2, 0, 8) {
                    res = true;
                    rep(i, 0, n){
                        if(f0[i][n-1] == 'X' && rott[iot[1]][i2][i][0] == 'X') res = false;
                        if(rott[iot[0]][i1][i][n-1] == 'X' && rott[iot[1]][i2][0][n-1-i] == 'X') res = false;
                    }
                    if(!res) continue;
                    rep(i3, 0, 8) {
                        res = true;
                        rep(i, 0, n){
                            if(f0[n-1][i] == 'X' && rott[iot[2]][i3][0][i] == 'X') res = false;
                            if(rott[iot[1]][i2][n-1][i] == 'X' && rott[iot[2]][i3][i][n-1] == 'X') res = false;
                        }
                        if(!res) continue;
                        rep(i4, 0, 8){
                            res = true;
                            rep(i, 0, n){
                                if(f0[i][0] == 'X' && rott[iot[3]][i4][i][n-1] == 'X') res = false;
                                if(rott[iot[2]][i3][i][0] == 'X' && rott[iot[3]][i4][n-1][n-1-i] == 'X') res = false;
                                if(rott[iot[3]][i4][0][i] == 'X' && rott[iot[0]][i1][i][0] == 'X') res = false;
                            }
                            if(!res) continue;
                            rep(i5, 0, 8){
                                res = true;
                                rep(i, 0, n){
                                    if(rott[iot[4]][i5][0][i] == 'X' && rott[iot[0]][i1][0][i] == 'X') res = false;
                                    if(rott[iot[4]][i5][n-1][i] == 'X' && rott[iot[2]][i3][n-1][i] == 'X') res = false;
                                    if(rott[iot[4]][i5][i][0] == 'X' && rott[iot[3]][i4][i][0] == 'X') res = false;
                                    if(rott[iot[4]][i5][i][n-1] == 'X' && rott[iot[1]][i2][i][n-1] == 'X') res = false;
                                }
                                if(!res) continue;
                                ans = true;
                            }
                            if(ans) break;
                        }
                        if(ans) break;
                    }
                    if(ans) break;
                }
                if(ans) break;
            }
            if(ans) break;
        } while(next_permutation(all(iot)));

        rep(i, 0, n) reverse(all(b[0][i]));
        sort(all(iot));
        do {
            vector<string> f0 = b[0];
            rep(i1, 0, 8) {
                bool res = true;
                rep(i, 0, n){
                    if(f0[0][i] == 'X' && rott[iot[0]][i1][n-1][i] == 'X') res = false;
                }
                if(!res) continue;
                rep(i2, 0, 8) {
                    res = true;
                    rep(i, 0, n){
                        if(f0[i][n-1] == 'X' && rott[iot[1]][i2][i][0] == 'X') res = false;
                        if(rott[iot[0]][i1][i][n-1] == 'X' && rott[iot[1]][i2][0][n-1-i] == 'X') res = false;
                    }
                    if(!res) continue;
                    rep(i3, 0, 8) {
                        res = true;
                        rep(i, 0, n){
                            if(f0[n-1][i] == 'X' && rott[iot[2]][i3][0][i] == 'X') res = false;
                            if(rott[iot[1]][i2][n-1][i] == 'X' && rott[iot[2]][i3][i][n-1] == 'X') res = false;
                        }
                        if(!res) continue;
                        rep(i4, 0, 8){
                            res = true;
                            rep(i, 0, n){
                                if(f0[i][0] == 'X' && rott[iot[3]][i4][i][n-1] == 'X') res = false;
                                if(rott[iot[2]][i3][i][0] == 'X' && rott[iot[3]][i4][n-1][n-1-i] == 'X') res = false;
                                if(rott[iot[3]][i4][0][i] == 'X' && rott[iot[0]][i1][i][0] == 'X') res = false;
                            }
                            if(!res) continue;
                            rep(i5, 0, 8){
                                res = true;
                                rep(i, 0, n){
                                    if(rott[iot[4]][i5][0][i] == 'X' && rott[iot[0]][i1][0][i] == 'X') res = false;
                                    if(rott[iot[4]][i5][n-1][i] == 'X' && rott[iot[2]][i3][n-1][i] == 'X') res = false;
                                    if(rott[iot[4]][i5][i][0] == 'X' && rott[iot[3]][i4][i][0] == 'X') res = false;
                                    if(rott[iot[4]][i5][i][n-1] == 'X' && rott[iot[1]][i2][i][n-1] == 'X') res = false;
                                }
                                if(!res) continue;
                                ans = true;
                            }
                            if(ans) break;
                        }
                        if(ans) break;
                    }
                    if(ans) break;
                }
                if(ans) break;
            }
            if(ans) break;
        } while(next_permutation(all(iot)));

        if(ans) {
            cout << "Yes" << endl;
        }else{
            cout << "No" << endl;
        }
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3584
final_rank: 14

code_snippet: // AOJ #1636 Cube Surface Puzzle
// 2025.4.1

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

vector<string> rotatePiece(const vector<string>& S) {
    int N = S.size();
    vector<string> R(N, string(N, ' '));
    for (int i = 0; i < N; i++){
        for (int j = 0; j < N; j++) R[j][N - 1 - i] = S[i][j];
    }
    return R;
}

vector<string> flipPiece(const vector<string>& S) {
    vector<string> F = S;
    for(auto &row : F) reverse(row.begin(), row.end());
    return F;
}

bool edgeMatch(const string &A, const string &B) {
    int L = A.size();
    for (int i = 0; i < L; i++) if(A[i]=='X' && B[i]=='X') return false;
    for (int i = 1; i < L - 1; i++) if(A[i]=='.' && B[i]=='.') return false;
    return true;
}

bool OK(char a, char b, char c) { return (a + b + c) == ('.' + '.' + 'X'); }

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

    while(true) {
        int N;
        cin >> N;
        if(N == 0) break;

        vector<vector<string>> S(6, vector<string>(N));
        for (int i = 0; i < 6; i++) for (int j = 0; j < N; j++) cin >> S[i][j];

        vector<vector<vector<string>>> T(6, vector<vector<string>>(8));
        for (int i = 0; i < 6; i++){
            T[i][0] = S[i];
            for (int j = 1; j < 4; j++) T[i][j] = rotatePiece(T[i][j - 1]);
            for (int j = 4; j < 8; j++) T[i][j] = flipPiece(T[i][j - 4]);
        }

        vector<vector<vector<string>>> X(6, vector<vector<string>>(8, vector<string>(8, "")));
        for (int i = 0; i < 6; i++){
            for (int o = 0; o < 8; o++){
                X[i][o][0] = T[i][o][0];
                X[i][o][1] = "";
                for (int k = 0; k < N; k++) X[i][o][1] += T[i][o][k][N - 1];
                X[i][o][2] = "";
                for (int k = 0; k < N; k++) X[i][o][2] += T[i][o][N - 1][N - 1 - k];
                X[i][o][3] = "";
                for (int k = 0; k < N; k++) X[i][o][3] += T[i][o][N - 1 - k][0];
                for (int k = 0; k < 4; k++){
                    X[i][o][k + 4] = X[i][o][k];
                    reverse(X[i][o][k + 4].begin(), X[i][o][k + 4].end());
                }
            }
        }

        bool found = false;
        for (int m0 = 0; m0 < 8 && !found; m0++){
            for (int d = 1; d < 6 && !found; d++){
                for (int md = 0; md < 8 && !found; md++){
                    int l = 1; if(l == d) l++;
                    for (int ml = 0; ml < 8 && !found; ml++){
                        if(!edgeMatch(X[0][m0][7], X[l][ml][0])) continue;
                        if(!edgeMatch(X[l][ml][6], X[d][md][7])) continue;
                        vector<int> B;
                        for (int k = 1; k < 6; k++) if(k != d && k != l) B.push_back(k);
                        for (int iB = 0; iB < (int)B.size() && !found; iB++){
                            int b = B[iB];
                            for (int mb = 0; mb < 8 && !found; mb++){
                                if(!edgeMatch(X[b][mb][0], X[0][m0][0])) continue;
                                if(!edgeMatch(X[b][mb][7], X[l][ml][7])) continue;
                                if(!edgeMatch(X[b][mb][6], X[d][md][0])) continue;
                                for (int iB2 = 0; iB2 < (int)B.size() && !found; iB2++){
                                    if(B[iB2] == b) continue;
                                    int r = B[iB2];
                                    for (int mr = 0; mr < 8 && !found; mr++){
                                        if(!edgeMatch(X[b][mb][5], X[r][mr][3])) continue;
                                        if(!edgeMatch(X[0][m0][1], X[r][mr][0])) continue;
                                        if(!edgeMatch(X[r][mr][6], X[d][md][1])) continue;
                                        int f = B[0] ^ B[1] ^ B[2] ^ b ^ r;
                                        for (int mf = 0; mf < 8 && !found; mf++){
                                            if(!edgeMatch(X[f][mf][0], X[0][m0][6])) continue;
                                            if(!edgeMatch(X[f][mf][1], X[r][mr][1])) continue;
                                            if(!edgeMatch(X[f][mf][2], X[d][md][2])) continue;
                                            if(!edgeMatch(X[f][mf][3], X[l][ml][5])) continue;
                                            if(!OK(T[0][m0][0][0], T[l][ml][0][0], T[b][mb][0][0])) continue;
                                            if(!OK(T[0][m0][0][N-1], T[b][mb][0][N-1], T[r][mr][0][0])) continue;
                                            if(!OK(T[0][m0][N-1][0], T[l][ml][0][N-1], T[f][mf][0][0])) continue;
                                            if(!OK(T[0][m0][N-1][N-1], T[r][mr][0][N-1], T[f][mf][0][N-1])) continue;
                                            if(!OK(T[d][md][0][0], T[l][ml][N-1][0], T[b][mb][N-1][0])) continue;
                                            if(!OK(T[d][md][0][N-1], T[b][mb][N-1][N-1], T[r][mr][N-1][0])) continue;
                                            if(!OK(T[d][md][N-1][0], T[l][ml][N-1][N-1], T[f][mf][N-1][0])) continue;
                                            if(!OK(T[d][md][N-1][N-1], T[f][mf][N-1][N-1], T[r][mr][N-1][N-1])) continue;

                                            found = true;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        cout << (found ? "Yes" : "No") << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3536
final_rank: 13

code_snippet: #if !defined(__clang__) && defined(__GNUC__)
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#ifdef ONLINE_JUDGE
#pragma GCC target("avx2,fma,popcnt,bmi,bmi2")
#endif
#elif defined(__clang__)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#else
#error "We don't know this compiler lol"
#endif
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
std::ostream &operator<<(std::ostream &os, const modint998244353 &a) {
    return os << a.val();
}
std::ostream &operator<<(std::ostream &os, const modint1000000007 &a) {
    return os << a.val();
}
std::istream &operator>>(std::istream &is, modint998244353 &a) {
    long long t;
    is >> t;
    a = t;
    return is;
}
std::istream &operator>>(std::istream &is, modint1000000007 &a) {
    long long t;
    is >> t;
    a = t;
    return is;
}
#endif
using namespace std;
// # Type Aliases
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <class T>
using V = vector<T>;
template <class T, class U>
using P = pair<T, U>;
template <class T>
using max_heap = priority_queue<T>;
template <class T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
// # Repeat Macros
#define OVERLOADER(_1, _2, _3, NAME, ...) NAME
#define REP2(i, a, b) for (ll i = (a); i < (ll)(b); ++i)
#define REP(i, n) REP2(i, 0, n)
#define rep(...) OVERLOADER(__VA_ARGS__, REP2, REP)(__VA_ARGS__)
#define repd(i, a, b) for (ll i = (ll)(b) - 1; i >= (ll)(a); --i)
// # Abbreviation Macros
#define pb push_back
#define eb emplace_back
#define ALL(a) begin(a), end(a)
#define RALL(a) a.rbegin(), a.rend()
// # Fast IO and IO Settings
struct IO_Setting {
    IO_Setting() {
        cin.tie(nullptr);
        ios_base::sync_with_stdio(false);
        cout << fixed << setprecision(15);
        cerr << fixed << setprecision(3);
    }
} io_setting;
// # Input Overloads
template <class T, class U>
istream &operator>>(istream &is, P<T, U> &p) {
    return is >> p.first >> p.second;
}
template <class T>
istream &operator>>(istream &is, V<T> &v) {
    for (T &e : v) is >> e;
    return is;
}
// # Output Overloads
template <class T, class U>
ostream &operator<<(ostream &os, const P<T, U> &p) {
    return os << p.first << ' ' << p.second;
}
template <class T>
ostream &operator<<(ostream &os, const V<T> &v) {
    int ss = v.size();
    rep(ii, ss) { os << v[ii] << (ii + 1 == ss ? "" : " "); }
    return os;
}
// # Function Definition
template <class T>
T sq(T x) {
    return x * x;
}
template <class T, class U>
inline bool chmin(T &a, U b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T, class U>
inline bool chmax(T &a, U b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
ll max(ll lhs, ll rhs) { return std::max<ll>(lhs, rhs); }
ll min(ll lhs, ll rhs) { return std::min<ll>(lhs, rhs); }
ll ceil_div(ll a, ll b) { return a / b + ((a ^ b) >= 0 && a % b); }
ll floor_div(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); }
template <class Container>
Container reversed(const Container &v) {
    return Container(v.rbegin(), v.rend());
}
template <class Container>
Container sorted(const Container &v, bool rev = false) {
    Container r = v;
    rev ? sort(r.rbegin(), r.rend()) : sort(r.begin(), r.end());
    return r;
}
template <class T>
void uniqify(V<T> &v) {
    v.erase(unique(ALL(v)), v.end());
}
template <class T>
V<T> compress(V<T> &v) {
    V<T> vals = v;
    sort(ALL(vals));
    uniqify(vals);
    rep(i, v.size()) v[i] = lower_bound(ALL(vals), v[i]) - vals.begin();
    return vals;
}
template <class T>
V<T> sum_array(const V<T> &v) {
    int n = v.size();
    V<T> sum(n + 1);
    rep(i, n) sum[i + 1] = sum[i] + v[i];
    return sum;
}
template <class T>
inline void drop(T &&x) {
    cout << x << endl;
    exit(0);
}
inline void yesno(bool f) { cout << (f ? "Yes" : "No") << '\n'; }
// # Constant Definition
constexpr char ENDL = '\n';
constexpr ll INF = (1LL << 30) - 1;    // 問題毎にfit
constexpr ll INFLL = (1LL << 62) - 1;  // 問題毎にfit
const ld PI = acos(static_cast<long double>(-1));
// constexpr int dy[] = {0, 1, 0, -1};
// constexpr int dx[] = {1, 0, -1, 0};
// constexpr int dy[]={0, 1, 1, 1, 0, -1, -1, -1};
// constexpr int dx[]={1, 1, 0, -1, -1, -1, 0, 1};
// # Debug Macro
#ifdef LOCAL
// #include "CP-library/debug_print.hpp"
// #define debug(...) debug_impl(__LINE__, __VA_ARGS__)
// #define debug_impl(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#include "cpp-dump/cpp-dump.hpp"
#define dump(...) cpp_dump(__VA_ARGS__)
#if __has_include(<atcoder/modint>)
#include <atcoder/modint>
namespace cpp_dump::_detail {
template <int m>
inline std::string export_var(const atcoder::static_modint<m> &mint,
                              const std::string &indent,
                              std::size_t last_line_length,
                              std::size_t current_depth, bool fail_on_newline,
                              const export_command &command) {
    return export_var(mint.val(), indent, last_line_length, current_depth,
                      fail_on_newline, command);
}
template <int m>
inline std::string export_var(const atcoder::dynamic_modint<m> &mint,
                              const std::string &indent,
                              std::size_t last_line_length,
                              std::size_t current_depth, bool fail_on_newline,
                              const export_command &command) {
    return export_var(mint.val(), indent, last_line_length, current_depth,
                      fail_on_newline, command);
}
}  // namespace cpp_dump::_detail
#endif  // __has_include(<atcoder/modint>)
namespace cp = cpp_dump;
CPP_DUMP_SET_OPTION_GLOBAL(max_line_width, 100);
CPP_DUMP_SET_OPTION_GLOBAL(log_label_func, cp::log_label::line());
// CPP_DUMP_DEFINE_EXPORT_OBJECT(Type, properties..., methods...);
#define INLOCAL(statements) \
    do {                    \
        statements          \
    } while (false)
#else
#define debug(...)
#define dump(...)
#define INLOCAL(statements)
#endif
// using mint = modint1000000007;
// using mint = modint998244353;

constexpr ll B = 9;
ll n;

struct Piece {
    bool grid[B][B];
    Piece() : grid() {}

    Piece flip() {
        Piece ret;
        rep(i, n) {
            rep(j, n) { ret.grid[i][j] = grid[i][n - j - 1]; }
        }
        return ret;
    }
    Piece clockwise_rotated() {
        Piece ret;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                ret.grid[i][j] = grid[n - j - 1][i];
            }
        }
        return ret;
    }
};
Piece pieces[6];
Piece transformed[6][8];

constexpr int PLAINS = 6;
// 絶対*(n-1)してください
constexpr int ofx[] = {0, 0, 0, 0, 1, 0};
constexpr int ofy[] = {0, 0, 0, 1, 0, 0};
constexpr int ofz[] = {0, 0, 0, 0, 0, 1};

constexpr int dix[] = {1, 1, 0, 1, 0, 1};
constexpr int diy[] = {0, 0, 1, 0, 1, 0};
constexpr int diz[] = {0, 0, 0, 0, 0, 0};
constexpr int djx[] = {0, 0, 0, 0, 0, 0};
constexpr int djy[] = {1, 0, 0, 0, 0, 1};
constexpr int djz[] = {0, 1, 1, 1, 1, 0};

struct Puzzle {
    int invalid_count;
    int filled_count;
    int cube_cnt[B][B][B];

    Puzzle() : invalid_count(), filled_count(), cube_cnt() {}

    void place(int plain, const Piece &piece) {
        const int pofx = ofx[plain] * (n - 1);
        const int pofy = ofy[plain] * (n - 1);
        const int pofz = ofz[plain] * (n - 1);
        rep(i, n) {
            rep(j, n) {
                int &target = cube_cnt[pofx + i * dix[plain] + j * djx[plain]]
                                      [pofy + i * diy[plain] + j * djy[plain]]
                                      [pofz + i * diz[plain] + j * djz[plain]];
                int prev = target;
                target += piece.grid[i][j];
                if (prev == 0 && target == 1) filled_count++;
                if (prev == 1 && target == 2) invalid_count++;
            }
        }
    }
    void unplace(int plain, const Piece &piece) {
        const int pofx = ofx[plain] * (n - 1);
        const int pofy = ofy[plain] * (n - 1);
        const int pofz = ofz[plain] * (n - 1);
        rep(i, n) {
            rep(j, n) {
                int &target = cube_cnt[pofx + i * dix[plain] + j * djx[plain]]
                                      [pofy + i * diy[plain] + j * djy[plain]]
                                      [pofz + i * diz[plain] + j * djz[plain]];
                int prev = target;
                target -= piece.grid[i][j];
                if (prev == 1 && target == 0) filled_count--;
                if (prev == 2 && target == 1) invalid_count--;
            }
        }
    }
    bool is_valid() { return invalid_count == 0; }
    bool is_satisfied() {
        return filled_count == n * n * n - (n - 2) * (n - 2) * (n - 2);
    }
};

#ifdef LOCAL
CPP_DUMP_DEFINE_EXPORT_OBJECT(Piece, grid);
CPP_DUMP_DEFINE_EXPORT_OBJECT(Puzzle, invalid_count, filled_count, cube_cnt);
#endif

bool solve() {
    Puzzle puzzle;
    // input
    rep(i, 6) {
        rep(j, n) {
            rep(k, n) {
                char c;
                cin >> c;
                pieces[i].grid[j][k] = (c == 'X');
            }
        }
    }
    // dump(pieces | cp::boolnum());

    // piece5をおく
    puzzle.place(5, pieces[5]);
    dump(puzzle);
    // puzzle.unplace(5, pieces[5]);
    // dump(puzzle);
    // return false; // debug

    // make transformed
    rep(i, 5) {
        transformed[i][0] = pieces[i];
        rep(j, 1, 4) {
            transformed[i][j] = transformed[i][j - 1].clockwise_rotated();
        }
        transformed[i][4] = transformed[i][3].clockwise_rotated().flip();
        rep(j, 5, 8) {
            transformed[i][j] = transformed[i][j - 1].clockwise_rotated();
        }
        // dump(i, transformed[i] | cp::boolnum());
    }

    vector<int> perm(5);
    iota(ALL(perm), 0);

    do {
        bool ok = false;
        // dfs
        auto dfs = [&](auto &f, int placed) -> void {
            if (placed == 5) {
                if (puzzle.is_satisfied()) {
                    ok = true;
                }
                return;
            }
            rep(j, 8) {
                puzzle.place(placed, transformed[perm[placed]][j]);
                if (puzzle.is_valid()) {
                    f(f, placed + 1);
                }
                puzzle.unplace(placed, transformed[perm[placed]][j]);
            }
        };
        dfs(dfs, 0);
        // if (!(puzzle.filled_count == 0 && puzzle.invalid_count == 0)) {
            // dump(ok, perm, puzzle);
            // assert(false);
        // }
        // assert(puzzle.filled_count == 0 && puzzle.invalid_count == 0);
        if (ok) return true;
    } while (next_permutation(ALL(perm)));
    return false;
}

int main() {
    while (true) {
        cin >> n;
        if (n == 0) break;
        cout << (solve() ? "Yes" : "No") << ENDL;
    }
}
accuracy: 1
time_ms: 620
memory_kb: 3480
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
typedef vector<vvvb> vvvvb;
typedef pair<ll,ll> pi;
typedef pair<ll,pi> ppi;
#define FOR(i,l,r) for(ll i=l;i<r;i++)
#define REP(i,n) FOR(i,0,n)
#define RFOR(i,l,r) for(ll i=r-1;i>=l;i--)
#define RREP(i,n) RFOR(i,0,n)
#define sz(A) (ll)(A.size())
#define ALL(A) A.begin(),A.end()
#define LB(A,x) (ll)(lower_bound(ALL(A),x)-A.begin())
#define UB(A,x) (ll)(upper_bound(ALL(A),x)-A.begin())
#define COU(A,x) (UB(A,x)-LB(A,x))
#define F first
#define S second
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>&p){os<<p.F<<" "<<p.S;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}
template<typename T>ostream&operator<<(ostream&os,vector<T>&v){REP(i,sz(v))os<<v[i]<<(i+1!=sz(v)?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
template<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}
const ll mod=998244353;
template<const long long int mod=998244353>
struct modint{
  using mint=modint<mod>;
  long long int x;
  modint(long long int _x=0):x(_x%mod){if(x<0)x+=mod;}
  long long int val(){return x;}
  mint&operator=(const mint&a){x=a.x;return *this;}
  mint&operator+=(const mint&a){x+=a.x;if(x>=mod)x-=mod;return *this;}
  mint&operator-=(const mint&a){x-=a.x;if(x<0)x+=mod;return *this;}
  mint&operator*=(const mint&a){x*=a.x;x%=mod;return *this;}
  friend mint operator+(const mint&a,const mint&b){return mint(a)+=b;}
  friend mint operator-(const mint&a,const mint&b){return mint(a)-=b;}
  friend mint operator*(const mint&a,const mint&b){return mint(a)*=b;}
  mint operator-()const{return mint(0)-*this;}
  mint pow(long long int n){
    if(!n)return 1;
    mint a=1;
    mint _x=x;
    while(n){
      if(n&1)a*=_x;
      _x=_x*_x;n>>=1;
    }
    return a;
  }
  mint inv(){return pow(mod-2);}
  mint&operator/=(mint&a){return *this*=a.inv();}
  friend mint operator/(const mint&a,mint b){return mint(a)/=b;}
};
using mint=modint<998244353>;
vector<string>rot(vector<string>S){
  auto T=S;
  REP(i,sz(S))REP(j,sz(S))T[i][j]=S[sz(S)-1-j][i];
  return T;
}
vector<string>rev(vector<string>S){
  auto T=S;
  REP(i,sz(T))reverse(ALL(T[i]));
  return T;
}
bool f(vector<string>&F,vector<string>&B,vector<string>&L,vector<string>&R,vector<string>&U,vector<string>&D){
  ll N=sz(F);
  REP(i,N){
    if(U[0][i]=='X'&&B[0][i]=='X')return 0;
    if(U[i][0]=='X'&&L[0][i]=='X')return 0;
    if(U[N-1][i]=='X'&&F[0][i]=='X')return 0;
    if(U[i][N-1]=='X'&&R[0][N-1-i]=='X')return 0;
    if(F[i][0]=='X'&&L[i][N-1]=='X')return 0;
    if(F[i][N-1]=='X'&&R[i][0]=='X')return 0;
    if(B[i][0]=='X'&&L[i][0]=='X')return 0;
    if(B[i][N-1]=='X'&&R[i][N-1]=='X')return 0;
    if(D[0][i]=='X'&&B[N-1][i]=='X')return 0;
    if(D[N-1][i]=='X'&&F[N-1][i]=='X')return 0;
    if(D[i][0]=='X'&&L[N-1][i]=='X')return 0;
    if(D[i][N-1]=='X'&&R[N-1][N-1-i]=='X')return 0;
  }
  return 1;
};
int main(){
    while(1){
        ll N;cin>>N;
        if(!N)return 0;
        vector<vector<string>>S(6,vector<string>(N));cin>>S;
        ll cnt=0;
        REP(i,6)REP(j,N)REP(k,N)if(S[i][j][k]=='X')cnt++;
        if(cnt!=N*N*N-(N-2)*(N-2)*(N-2)){cout<<"No"<<endl;continue;}
        vector<vector<vector<string>>>T(6,vector<vector<string>>(8,vector<string>(N)));
        REP(i,6){
          T[i][0]=S[i];
          FOR(j,1,4)T[i][j]=rot(T[i][j-1]);
          T[i][4]=rev(S[i]);
          FOR(j,5,8)T[i][j]=rot(T[i][j-1]);
        }
        bool ok=0;
        for(auto&S0:T[0])REP(_,5){
            for(auto&S1:T[1]){
              for(auto&S2:T[2]){
                for(auto&S3:T[3]){
                  for(auto&S4:T[4]){
                    for(auto&S5:T[5]){
                      if(f(S0,S1,S2,S3,S4,S5))ok=1;
                      if(f(S0,S1,S2,S3,S5,S4))ok=1;
                      if(f(S0,S1,S2,S4,S3,S5))ok=1;
                      if(f(S0,S1,S2,S4,S5,S3))ok=1;
                      if(f(S0,S1,S2,S5,S3,S4))ok=1;
                      if(f(S0,S1,S2,S5,S4,S3))ok=1;
                    }
                  }
                }
              }
            }
            rotate(S.begin()+1,S.begin()+2,S.end());
            rotate(T.begin()+1,T.begin()+2,T.end());
          }
        cout<<(ok?"Yes":"No")<<endl;
    }
    return 0;
}
accuracy: 1
time_ms: 6790
memory_kb: 3416
final_rank: 19

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */


int N;
int MAX;
int PV[6][6][8] = {}; //piece i wo surface j ni muki k de haru
int PE[6][6][8][12] = {}; //piece i wo surface j ni muki k de haru

const int SV[6][4] = {{0,1,3,2},{0,1,4,5},{1,2,5,6},{3,2,7,6},{0,3,4,7},{4,5,7,6}};
const int SE[6][4] = {{0,3,2,1},{0,4,8,5},{1,5,9,6},{2,7,10,6},{3,4,11,7},{8,11,10,9}};

vector<vector<pair<int,int>>> Share(6);

vector<vector<bool>> Rotate(vector<vector<bool>> B) {
    vector<vector<bool>> Ret(N,vector<bool>(N));
    rep(i,0,N) {
        rep(j,0,N) {
            Ret[N-1-j][i] = B[i][j];
        }
    }
    return Ret;
}

vector<vector<bool>> Flip(vector<vector<bool>> B) {
    vector<vector<bool>> Ret(N,vector<bool>(N));
    rep(i,0,N) {
        rep(j,0,N) {
            Ret[N-1-i][j] = B[i][j];
        }
    }
    return Ret;
}

bool DFS(int Level, vector<bool>& used, vector<pair<int,int>>& Cur, int Ver) { //Cur[i] : surface i ni piece first muki second
    if (Level == 6) {
        if (Ver == 255) return true;
        else return false;
    }
    rep(i,0,6) {
        if (used[i]) continue;
        rep(j,0,8) {
            bool check = true;
            if ((Ver & PV[i][Level][j]) != 0) check = false;
            for (pair<int,int> k : Share[Level]) {
                if ((PE[i][Level][j][k.first] ^ PE[Cur[k.second].first][k.second][Cur[k.second].second][k.first]) != MAX) check = false;
            }
            if (!check) continue;
            used[i] = true;
            Cur.push_back({i,j});
            if (DFS(Level+1,used,Cur,Ver|PV[i][Level][j])) return true;
            used[i] = false;
            Cur.pop_back();
        }
    }
    return false;
}

int main() {
Share[0] = {}, Share[1] = {{0,0}}, Share[2] = {{1,0},{5,1}}, Share[3] = {{2,0},{6,2}}, Share[4] = {{3,0},{4,1},{7,3}}, Share[5] = {{8,1},{9,2},{10,3},{11,4}};
while(1) {
    cin >> N;
    if (N == 0) return 0;
    rep(i,0,6) rep(j,0,6) rep(k,0,8) PV[i][j][k] = 0;
    rep(i,0,6) rep(j,0,6) rep(k,0,8) rep(l,0,12) PE[i][j][k][l] = 0;
    MAX = (1<<(N-2)) - 1;
    rep(i,0,6) {
        vector<vector<bool>> Piece(N,vector<bool>(N));
        rep(j,0,N) {
            string S;
            cin >> S;
            rep(k,0,N) {
                if (S[k] == '.') Piece[j][k] = 0;
                else Piece[j][k] = 1;
            }
        }
        rep(j,0,4) {
            rep(k,0,6) {
                if (Piece[0][0]) PV[i][k][j] += (1<<SV[k][0]);
                if (Piece[N-1][0]) PV[i][k][j] += (1<<SV[k][1]);
                if (Piece[0][N-1]) PV[i][k][j] += (1<<SV[k][2]);
                if (Piece[N-1][N-1]) PV[i][k][j] += (1<<SV[k][3]);
                rep(l,1,N-1) {
                    if (Piece[l][0]) PE[i][k][j][SE[k][0]] += (1<<(l-1));
                    if (Piece[0][l]) PE[i][k][j][SE[k][1]] += (1<<(l-1));
                    if (Piece[l][N-1]) PE[i][k][j][SE[k][2]] += (1<<(l-1));
                    if (Piece[N-1][l]) PE[i][k][j][SE[k][3]] += (1<<(l-1));
                }
            }
            Piece = Rotate(Piece);
        }
        Piece = Flip(Piece);
        rep(j,4,8) {
            rep(k,0,6) {
                if (Piece[0][0]) PV[i][k][j] += (1<<SV[k][0]);
                if (Piece[N-1][0]) PV[i][k][j] += (1<<SV[k][1]);
                if (Piece[0][N-1]) PV[i][k][j] += (1<<SV[k][2]);
                if (Piece[N-1][N-1]) PV[i][k][j] += (1<<SV[k][3]);
                rep(l,1,N-1) {
                    if (Piece[l][0]) PE[i][k][j][SE[k][0]] += (1<<(l-1));
                    if (Piece[0][l]) PE[i][k][j][SE[k][1]] += (1<<(l-1));
                    if (Piece[l][N-1]) PE[i][k][j][SE[k][2]] += (1<<(l-1));
                    if (Piece[N-1][l]) PE[i][k][j][SE[k][3]] += (1<<(l-1));
                }
            }
            Piece = Rotate(Piece);
        }
    }
    vector<bool> used(6,false);
    vector<pair<int,int>> Cur = {};
    bool ANS = DFS(0,used,Cur,0);
    cout << (ANS ? "Yes" : "No") << endl;
}
}
accuracy: 1
time_ms: 270
memory_kb: 3424
final_rank: 5

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

#include <unistd.h>

namespace fastio {
static constexpr uint32_t SZ = 1 << 17;
char ibuf[SZ];
char obuf[SZ];
char out[100];
// pointer of ibuf, obuf


uint32_t pil = 0, pir = 0, por = 0;

struct Pre {
    char num[10000][4];
    constexpr Pre() : num() {
        for (int i = 0; i < 10000; i++) {
            int n = i;
            for (int j = 3; j >= 0; j--) {
                num[i][j] = n % 10 | '0';
                n /= 10;
            }
        }
    }
} constexpr pre;

inline void load() {
    memmove(ibuf, ibuf + pil, pir - pil);
    pir = pir - pil + fread(ibuf + pir - pil, 1, SZ - pir + pil, stdin);
    pil = 0;
    if (pir < SZ)
        ibuf[pir++] = '\n';
}

inline void flush() {
    fwrite(obuf, 1, por, stdout);
    por = 0;
}

void rd(char &c) {
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
}

void rd(string &x) {
    x.clear();
    char c;
    do {
        if (pil + 1 > pir)
            load();
        c = ibuf[pil++];
    } while (isspace(c));
    do {
        x += c;
        if (pil == pir)
            load();
        c = ibuf[pil++];
    } while (!isspace(c));
}

template <typename T> void rd_real(T &x) {
    string s;
    rd(s);
    x = stod(s);
}

template <typename T> void rd_integer(T &x) {
    if (pil + 100 > pir)
        load();
    char c;
    do
        c = ibuf[pil++];
    while (c < '-');
    bool minus = 0;
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (c == '-') {
            minus = 1, c = ibuf[pil++];
        }
    }
    x = 0;
    while ('0' <= c) {
        x = x * 10 + (c & 15), c = ibuf[pil++];
    }
    if constexpr (is_signed<T>::value || is_same_v<T, i128>) {
        if (minus)
            x = -x;
    }
}

void rd(int &x) {
    rd_integer(x);
}
void rd(ll &x) {
    rd_integer(x);
}
void rd(i128 &x) {
    rd_integer(x);
}
void rd(uint &x) {
    rd_integer(x);
}
void rd(ull &x) {
    rd_integer(x);
}
void rd(u128 &x) {
    rd_integer(x);
}
void rd(double &x) {
    rd_real(x);
}
void rd(long double &x) {
    rd_real(x);
}

template <class T, class U> void rd(pair<T, U> &p) {
    return rd(p.first), rd(p.second);
}
template <size_t N = 0, typename T> void rd_tuple(T &t) {
    if constexpr (N < std::tuple_size<T>::value) {
        auto &x = std::get<N>(t);
        rd(x);
        rd_tuple<N + 1>(t);
    }
}
template <class... T> void rd(tuple<T...> &tpl) {
    rd_tuple(tpl);
}

template <size_t N = 0, typename T> void rd(array<T, N> &x) {
    for (auto &d : x)
        rd(d);
}
template <class T> void rd(vector<T> &x) {
    for (auto &d : x)
        rd(d);
}

void read() {}
template <class H, class... T> void read(H &h, T &...t) {
    rd(h), read(t...);
}

void wt(const char c) {
    if (por == SZ)
        flush();
    obuf[por++] = c;
}
void wt(const string s) {
    for (char c : s)
        wt(c);
}
void wt(const char *s) {
    size_t len = strlen(s);
    for (size_t i = 0; i < len; i++)
        wt(s[i]);
}

template <typename T> void wt_integer(T x) {
    if (por > SZ - 100)
        flush();
    if (x < 0) {
        obuf[por++] = '-', x = -x;
    }
    int outi;
    for (outi = 96; x >= 10000; outi -= 4) {
        memcpy(out + outi, pre.num[x % 10000], 4);
        x /= 10000;
    }
    if (x >= 1000) {
        memcpy(obuf + por, pre.num[x], 4);
        por += 4;
    } else if (x >= 100) {
        memcpy(obuf + por, pre.num[x] + 1, 3);
        por += 3;
    } else if (x >= 10) {
        int q = (x * 103) >> 10;
        obuf[por] = q | '0';
        obuf[por + 1] = (x - q * 10) | '0';
        por += 2;
    } else
        obuf[por++] = x | '0';
    memcpy(obuf + por, out + outi + 4, 96 - outi);
    por += 96 - outi;
}

template <typename T> void wt_real(T x) {
    ostringstream oss;
    oss << fixed << setprecision(15) << double(x);
    string s = oss.str();
    wt(s);
}

void wt(int x) {
    wt_integer(x);
}
void wt(ll x) {
    wt_integer(x);
}
void wt(i128 x) {
    wt_integer(x);
}
void wt(uint x) {
    wt_integer(x);
}
void wt(ull x) {
    wt_integer(x);
}
void wt(u128 x) {
    wt_integer(x);
}
void wt(double x) {
    wt_real(x);
}
void wt(long double x) {
    wt_real(x);
}

template <class T, class U> void wt(const pair<T, U> val) {
    wt(val.first);
    wt(' ');
    wt(val.second);
}
template <size_t N = 0, typename T> void wt_tuple(const T t) {
    if constexpr (N < std::tuple_size<T>::value) {
        if constexpr (N > 0) {
            wt(' ');
        }
        const auto x = std::get<N>(t);
        wt(x);
        wt_tuple<N + 1>(t);
    }
}
template <class... T> void wt(tuple<T...> tpl) {
    wt_tuple(tpl);
}
template <class T, size_t S> void wt(const array<T, S> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}
template <class T> void wt(const vector<T> val) {
    auto n = val.size();
    for (size_t i = 0; i < n; i++) {
        if (i)
            wt(' ');
        wt(val[i]);
    }
}

void print() {
    wt('\n');
}
template <class Head, class... Tail> void print(Head &&head, Tail &&...tail) {
    wt(head);
    if (sizeof...(Tail))
        wt(' ');
    print(forward<Tail>(tail)...);
}
void __attribute__((destructor)) _d() {
    flush();
}
} // namespace fastio


using fastio::flush;
using fastio::print;
using fastio::read;

inline void first(bool i = true) {
    print(i ? "first" : "second");
}
inline void Alice(bool i = true) {
    print(i ? "Alice" : "Bob");
}
inline void Takahashi(bool i = true) {
    print(i ? "Takahashi" : "Aoki");
}
inline void yes(bool i = true) {
    print(i ? "yes" : "no");
}
inline void Yes(bool i = true) {
    print(i ? "Yes" : "No");
}
inline void No() {
    print("No");
}
inline void YES(bool i = true) {
    print(i ? "YES" : "NO");
}
inline void NO() {
    print("NO");
}
inline void Yay(bool i = true) {
    print(i ? "Yay!" : ":(");
}
inline void Possible(bool i = true) {
    print(i ? "Possible" : "Impossible");
}
inline void POSSIBLE(bool i = true) {
    print(i ? "POSSIBLE" : "IMPOSSIBLE");
}

/**
 * @brief Fast IO
 */


int N;
int MAX;
int PV[6][6][8] = {}; //piece i wo surface j ni muki k de haru
int PE[6][6][8][12] = {}; //piece i wo surface j ni muki k de haru

const int SV[6][4] = {{0,1,3,2},{0,1,4,5},{1,2,5,6},{3,2,7,6},{0,3,4,7},{4,5,7,6}};
const int SE[6][4] = {{0,3,2,1},{0,4,8,5},{1,5,9,6},{2,7,10,6},{3,4,11,7},{8,11,10,9}};

vector<vector<pair<int,int>>> Share(6);

vector<vector<bool>> Rotate(vector<vector<bool>> B) {
    vector<vector<bool>> Ret(N,vector<bool>(N));
    rep(i,0,N) {
        rep(j,0,N) {
            Ret[N-1-j][i] = B[i][j];
        }
    }
    return Ret;
}

vector<vector<bool>> Flip(vector<vector<bool>> B) {
    vector<vector<bool>> Ret(N,vector<bool>(N));
    rep(i,0,N) {
        rep(j,0,N) {
            Ret[N-1-i][j] = B[i][j];
        }
    }
    return Ret;
}

bool DFS(int Level, vector<bool>& used, vector<pair<int,int>>& Cur, int Ver) { //Cur[i] : surface i ni piece first muki second
    if (Level == 6) {
        if (Ver == 255) return true;
        else return false;
    }
    rep(i,0,6) {
        if (used[i]) continue;
        rep(j,0,8) {
            bool check = true;
            if ((Ver & PV[i][Level][j]) != 0) check = false;
            for (pair<int,int> k : Share[Level]) {
                if ((PE[i][Level][j][k.first] ^ PE[Cur[k.second].first][k.second][Cur[k.second].second][k.first]) != MAX) check = false;
            }
            if (!check) continue;
            used[i] = true;
            Cur.push_back({i,j});
            if (DFS(Level+1,used,Cur,Ver|PV[i][Level][j])) return true;
            used[i] = false;
            Cur.pop_back();
        }
    }
    return false;
}

int main() {
Share[0] = {}, Share[1] = {{0,0}}, Share[2] = {{1,0},{5,1}}, Share[3] = {{2,0},{6,2}}, Share[4] = {{3,0},{4,1},{7,3}}, Share[5] = {{8,1},{9,2},{10,3},{11,4}};
while(1) {
    cin >> N;
    if (N == 0) return 0;
    rep(i,0,6) rep(j,0,6) rep(k,0,8) PV[i][j][k] = 0;
    rep(i,0,6) rep(j,0,6) rep(k,0,8) rep(l,0,12) PE[i][j][k][l] = 0;
    MAX = (1<<(N-2)) - 1;
    rep(i,0,6) {
        vector<vector<bool>> Piece(N,vector<bool>(N));
        rep(j,0,N) {
            string S;
            cin >> S;
            rep(k,0,N) {
                if (S[k] == '.') Piece[j][k] = 0;
                else Piece[j][k] = 1;
            }
        }
        rep(j,0,4) {
            rep(k,0,6) {
                if (Piece[0][0]) PV[i][k][j] += (1<<SV[k][0]);
                if (Piece[N-1][0]) PV[i][k][j] += (1<<SV[k][1]);
                if (Piece[0][N-1]) PV[i][k][j] += (1<<SV[k][2]);
                if (Piece[N-1][N-1]) PV[i][k][j] += (1<<SV[k][3]);
                rep(l,1,N-1) {
                    if (Piece[l][0]) PE[i][k][j][SE[k][0]] += (1<<(l-1));
                    if (Piece[0][l]) PE[i][k][j][SE[k][1]] += (1<<(l-1));
                    if (Piece[l][N-1]) PE[i][k][j][SE[k][2]] += (1<<(l-1));
                    if (Piece[N-1][l]) PE[i][k][j][SE[k][3]] += (1<<(l-1));
                }
            }
            Piece = Rotate(Piece);
        }
        Piece = Flip(Piece);
        rep(j,4,8) {
            rep(k,0,6) {
                if (Piece[0][0]) PV[i][k][j] += (1<<SV[k][0]);
                if (Piece[N-1][0]) PV[i][k][j] += (1<<SV[k][1]);
                if (Piece[0][N-1]) PV[i][k][j] += (1<<SV[k][2]);
                if (Piece[N-1][N-1]) PV[i][k][j] += (1<<SV[k][3]);
                rep(l,1,N-1) {
                    if (Piece[l][0]) PE[i][k][j][SE[k][0]] += (1<<(l-1));
                    if (Piece[0][l]) PE[i][k][j][SE[k][1]] += (1<<(l-1));
                    if (Piece[l][N-1]) PE[i][k][j][SE[k][2]] += (1<<(l-1));
                    if (Piece[N-1][l]) PE[i][k][j][SE[k][3]] += (1<<(l-1));
                }
            }
            Piece = Rotate(Piece);
        }
    }
    vector<bool> used(6,false);
    vector<pair<int,int>> Cur = {};
    used[0] = true;
    Cur.push_back({0,0});
    bool ANS = DFS(1,used,Cur,PV[0][0][0]);
    cout << (ANS ? "Yes" : "No") << endl;
}
}
accuracy: 1
time_ms: 10
memory_kb: 3516
final_rank: 9

code_snippet: #if 1
// clang-format off
#include <bits/stdc++.h>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using lf = long double;
using pll = pair<ll, ll>;
#define vec vector
template <class T> using v = vector<T>;
template <class T> using vv = v<v<T>>;
template <class T> using vvv = v<vv<T>>;
using vl = v<ll>;
using vvl = vv<ll>;
using vvvl = vvv<ll>;
using vpl = v<pll>;
using vs = v<string>;
using vb = v<bool>;
using vvb = v<vb>;
using vvvb = v<vvb>;
template<class T> using PQ = priority_queue<T, v<T>, greater<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

#define FOR(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, N) for (ll i = 0; i < (ll)(N); i++)
#define rep1(i, N) for (ll i = 1; i <= (ll)(N); i++)
#define rrep(i, N) for (ll i = N - 1; i >= 0; i--)
#define rrep1(i, N) for (ll i = N; i > 0; i--)
#define fore(i, a) for (auto &i : a)
#define fs first
#define sc second
#define eb emplace_back
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define UNIQUE(x) (x).erase(unique((x).begin(), (x).end()), (x).end());
#define YES(x) cout << ((x) ? "YES\n" : "NO\n");
#define Yes(x) cout << ((x) ? "Yes\n" : "No\n");
#define yes(x) cout << ((x) ? "yes\n" : "no\n");
template <class T, class U> void chmin(T &t, const U &u) { if (t > u) t = u; }
template <class T, class U> void chmax(T &t, const U &u) { if (t < u) t = u; }
template <class T> T min(const v<T> &lis) { return *min_element(all(lis)); }
template <class T> T max(v<T> &lis) { return *max_element(all(lis)); }
const int inf = (1 << 30);
const ll infl = (1LL << 60);
const ll mod93 = 998244353;
const ll mod17 = 1000000007;
int popcnt(uint x) { return __builtin_popcount(x); }
int popcnt(ull x) { return __builtin_popcountll(x); }
// 桁数
int bsr(uint x) { return 31 - __builtin_clz(x); }
int bsr(ull x) { return 63 - __builtin_clzll(x); }
// 2で割れる回数
int bsf(uint x) { return __builtin_ctz(x); }
int bsf(ull x) { return __builtin_ctzll(x); }

template <class T, class S> istream &operator>>(istream &is, pair<T, S> &x) { return is >> x.first >> x.second; }
template <class T, class S> ostream &operator<<(ostream &os, const pair<T, S> &x) { return os << x.first << " " << x.second; }
template <class T> istream &operator>>(istream &is, vector<T> &x) { for (auto &y : x) is >> y; return is; }
template <class T> ostream &operator<<(ostream &os, const vector<T> &x) {
  for (size_t i = 0, size = x.size(); i < size; i++)
    os << x[i] << (i == size - 1 ? "" : " ");
  return os;
}

ll rand_int(ll l, ll r) {  // [l, r]
  static random_device rd;
  static mt19937 gen(rd());
  return uniform_int_distribution<ll>(l, r)(gen);
}

// #include <boost/multiprecision/cpp_int.hpp>
// using cpp_int = boost::multiprecision::cpp_int;

// clang-format on
#endif

// #define _GLIBCXX_DEBUG

constexpr int N_MAX = 9;
int n, m;
using Piece = array<array<char, N_MAX>, N_MAX>;
Piece x[6];
Piece y[6][2][4];
Piece Piece_rotate(Piece p) {
  Piece ret;
  rep(i, n) rep(j, n) { ret[j][n - 1 - i] = p[i][j]; }
  return ret;
}
Piece Piece_reverse(Piece p) {
  Piece ret;
  rep(i, n) rep(j, n) ret[i][j] = p[j][i];
  return ret;
}
void print(const Piece &p) {
  rep(i, n) rep(j, n) cout << p[i][j] << " \n"[j + 1 == n];
}
using State = array<array<array<char, N_MAX>, N_MAX>, N_MAX>;

void print(const State &s) {
  rep(i, n) {
    cout << "i: " << i << "\n";
    rep(j, n) {
      rep(k, n) { cout << s[k][i][j] << " \n"[k + 1 == n]; }
    }
  }
}
State e() {
  State ret;
  rep(i, n) rep(j, n) rep(k, n) ret[i][j][k] = '.';
  return ret;
}

pair<bool, State> put(const State &s, const Piece &p, int i) {
  array<array<int, 9>, 6> d = {
      array<int, 9>{    0,     0,     0,  1,  0, 0, 0, 1, 0},
      array<int, 9>{    0,     0,     0,  1,  0, 0, 0, 0, 1},
      array<int, 9>{n - 1,     0,     0,  0,  1, 0, 0, 0, 1},
      array<int, 9>{n - 1, n - 1,     0, -1,  0, 0, 0, 0, 1},
      array<int, 9>{    0, n - 1,     0,  0, -1, 0, 0, 0, 1},
      array<int, 9>{    0,     0, n - 1,  1,  0, 0, 0, 1, 0},
  };
  State ret = s;
  int sx = d[i][0];
  int sy = d[i][1];
  int sz = d[i][2];
  // cout << "i, sx, sy, sz: " << i << " " << sx << " " << sy << " " << sz <<
  // endl;
  rep(j, n) {
    rep(k, n) {
      int x = sx + j * d[i][3] + k * d[i][6];
      int y = sy + j * d[i][4] + k * d[i][7];
      int z = sz + j * d[i][5] + k * d[i][8];
      // cout << "i, x, y, z: " << i << " " << x << " " << y << " " << z <<
      // endl;
      if (ret[x][y][z] == 'X' && p[j][k] == 'X') {
        return {true, ret};
      }
      if (ret[x][y][z] == '.' && p[j][k] == 'X')
        ret[x][y][z] = p[j][k];
    }
  }
  // cout << "i: " << i << "\n";
  // cout << "s\n";
  // print(s);
  // cout << "p\n";
  // print(p);
  // cout << "ret\n";
  // print(ret);
  return {false, ret};
}

bool ans = false;
void dfs(int i, bitset<6> used, State s) {
  if (i == 0) {
    State ns = put(s, y[0][0][0], 0).sc;
    dfs(1, 1, ns);
    return;
  }
  if (i == 6) {
    ans = true;
    // print(s);
    return;
  }
  rep(j, 6) {
    if (used[j]) continue;
    rep(reverse_num, 2) rep(rotate_num, 4) {
      if (ans) return;
      auto [ng, ns] = put(s, y[j][reverse_num][rotate_num], i);
      if (ng) continue;
      dfs(i + 1, used | bitset<6>(1 << j), ns);
    }
  }
}
void solve() {
  m = n * n * n;
  m -= (n - 2) * (n - 2) * (n - 2);
  int cnt = 0;
  rep(i, 6) {
    rep(j, n) {
      rep(k, n) {
        cin >> x[i][j][k];
        cnt += x[i][j][k] == 'X';
      }
    }
    y[i][0][0] = x[i];
    rep(j, 3) y[i][0][j + 1] = Piece_rotate(y[i][0][j]);
    rep(j, 4) y[i][1][j] = Piece_reverse(y[i][0][j]);
  }
  if (cnt != m) {
    Yes(false);
    return;
  }

  ans = false;
  State tmp = e();
  dfs(0, bitset<6>(0), tmp);
  Yes(ans);
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  while (true) {
    cin >> n;
    if (n == 0) break;
    solve();
  }
}
accuracy: 1
time_ms: 50
memory_kb: 3336
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

bool is_end = false;

int N;
using cover_t = vector<vector<vector< vector<array<int, 3>> >>>;
using blocks_t = vector<vector<vector< int >>>;

void rotate(vector<vector<int>> &vec)
{
  vector<vector<int>> tmp = vec;
  for (int i = 0; i < N; ++i)
  {
    for (int j = 0; j < N; ++j)
    {
      vec[i][j] = tmp[j][N-1-i];
    }
  }
  return;
}

void rever(vector<vector<int>> &vec)
{
  vector<vector<int>> tmp = vec;
  for (int i = 0; i < N; ++i)
  {
    for (int j = 0; j < N; ++j)
    {
      vec[i][j] = tmp[N-1-i][j];
    }
  }
  return;
}

void deb(blocks_t &blocks, cover_t &cover)
{
  for (int z = 0; z < N; ++z)
  {
    for (int x = 0; x < N; ++x)
    {
      for (int y = 0; y < N; ++y)
      {
        int sum = 0;
        for (auto [id, px, py] : cover[x][y][z]) sum += blocks[id][px][py];
        cout << sum << " ";
      }
      cout << endl;
    }
  }
  cout << "-------" << endl;
  
  return;
}

bool check_all(blocks_t &blocks, cover_t &cover)
{
  for (int x = 0; x < N; ++x)
  {
    for (int y = 0; y < N; ++y)
    {
      for (int z = 0; z < N; ++z)
      {
        bool isin = (1 <= min({x, y, z}) && max({x, y, z}) < N-1);
        int sum = 0;
        for (auto [id, px, py] : cover[x][y][z]) sum += blocks[id][px][py];
        if (isin && sum != 0) return false;
        if (!isin && sum != 1) return false;
      }
    }
  }
  
  return true;
}

bool check_mid(blocks_t &blocks, cover_t &cover)
{
  // low
  {
    int z = 0;
    for (int x = 0; x < N; ++x)
    {
      for (int y = 0; y < N; ++y)
      {
        int sum = 0;
        for (auto [id, px, py] : cover[x][y][z]) sum += blocks[id][px][py];
        if (sum != 1) return false;
      }
    }
  }
  // middle
  for (int z = 1; z < N-1; ++z)
  {
    {
      int sum = 0;
      for (int x = 0; x < N; ++x)
      {
        sum = 0;
        for (auto [id, px, py] : cover[x][0][z]) sum += blocks[id][px][py];
        if (sum != 1) return false;
        
        sum = 0;
        for (auto [id, px, py] : cover[x][N-1][z]) sum += blocks[id][px][py];
        if (sum != 1) return false;
      }
      for (int y = 0; y < N; ++y)
      {
        sum = 0;
        for (auto [id, px, py] : cover[0][y][z]) sum += blocks[id][px][py];
        if (sum != 1) return false;
        
        sum = 0;
        for (auto [id, px, py] : cover[N-1][y][z]) sum += blocks[id][px][py];
        if (sum != 1) return false;
      }
    }
  }
  
  return true;
}

bool check_bottom(int depth, blocks_t &blocks, cover_t &cover)
{
  if (depth == 5) return check_mid(blocks, cover);
  
  bool ret = true;
  if (2 <= depth)
  {
    int x = 1, y = 0, z = 0;
    for (x = 1; x < N-1; ++x)
    {
      int sum = 0;
      for (auto [id, px, py] : cover[x][y][z]) sum += blocks[id][px][py];
      if (sum != 1) return false;
    }
  }
  if (3 <= depth)
  {
    int x = N-1, y = 0, z = 0;
    for (y = 0; y < N-1; ++y)
    {
      int sum = 0;
      for (auto [id, px, py] : cover[x][y][z]) sum += blocks[id][px][py];
      if (sum != 1) return false;
    }
  }
  if (4 <= depth)
  {
    int x = N-1, y = N-1, z = 0;
    for (x = N-1; x > 0; --x)
    {
      int sum = 0;
      for (auto [id, px, py] : cover[x][y][z]) sum += blocks[id][px][py];
      if (sum != 1) return false;
    }
  }
  
  return true;
}

mt19937 engine(42);

bool dfs(int depth, blocks_t &blocks, cover_t &cover)
{
  if (depth == 6)
  {
    return check_all(blocks, cover);
  }
  
  if (!check_bottom(depth, blocks, cover)) return false;
  
  bool ret = false;
  for (int rev = 0; rev < 2; ++rev)
  {
    for (int ro = 0; ro < 4; ++ro)
    {
      ret |= dfs(depth + 1, blocks, cover);
      if (ret) return true;
      
      rotate(blocks[depth]);
    }
    rever(blocks[depth]);
  }
  
  return ret;
}



void solve()
{
  cin >> N;
  if (N == 0)
  {
    is_end = true;
    return;
  }
  vector blocks(6, vector(N, vector<int>(N, 0)));
  int Xsum = 0;
  for (int i = 0; i < 6; ++i)
  {
    for (int j = 0; j < N; ++j)
    {
      string S; cin >> S;
      for (int k = 0; k < N; ++k)
      {
        if (S[k] == 'X') blocks[i][k][N-1-j] = 1, Xsum++;
      }
    }
  }
  
  vector cover(N, vector(N, vector(N, vector<array<int, 3>>())));
  vector< array<array<int, 3>, 3> > diff;
  {
    // (sx, sy, sz), (dx, dy, dz), (dx, dy, dz)
    array<int, 3> ar0, ar1, ar2;
    array< array<int, 3>, 3> arr;
    
    ar0 = {0, 0, 0}, ar1 = {1, 0, 0}, ar2 = {0, 1, 0};
    diff.push_back({ar0, ar1, ar2});
    ar0 = {0, 0, 0}, ar1 = {1, 0, 0}, ar2 = {0, 0, 1};
    diff.push_back({ar0, ar1, ar2});
    ar0 = {N-1, 0, 0}, ar1 = {0, 1, 0}, ar2 = {0, 0, 1};
    diff.push_back({ar0, ar1, ar2});
    ar0 = {N-1, N-1, 0}, ar1 = {-1, 0, 0}, ar2 = {0, 0, 1};
    diff.push_back({ar0, ar1, ar2});
    ar0 = {0, N-1, 0}, ar1 = {0, -1, 0}, ar2 = {0, 0, 1};
    diff.push_back({ar0, ar1, ar2});
    ar0 = {0, 0, N-1}, ar1 = {1, 0, 0}, ar2 = {0, 1, 0};
    diff.push_back({ar0, ar1, ar2});
  }
  vector<array<int, 3>> outer;
  for (int i = 0; i < 6; ++i)
  {
    auto [s, dx, dy] = diff[i];
    for (int x = 0; x < N; ++x)
    {
      for (int y = 0; y < N; ++y)
      {
        int nx = s[0] + dx[0] * x + dy[0] * y;
        int ny = s[1] + dx[1] * x + dy[1] * y;
        int nz = s[2] + dx[2] * x + dy[2] * y;
        assert(0 <= min({nx, ny, nz}) && max({nx, ny, nz}) < N);
        cover[nx][ny][nz].push_back({i, x, y});
        outer.push_back({nx, ny, nz});
      }
    }
  }
  
  // for (int x = 0; x < N; ++x)
  // {
  //   for (int y = 0; y < N; ++y)
  //   {
  //     for (int z = 0; z < N; ++z)
  //     {
  //       if (N == 3)
  //       {
  //         cout << x << " " << y << " " << z << " " << cover[x][y][z].size() << endl;
  //         for (auto [i, px, py] : cover[x][y][z])
  //         {
  //           cout << i << " " << px << " " << py << endl;
  //         }
  //         cout << "--------" << endl;
  //       }
  //     }
  //   }
  // }
  sort(outer.begin(), outer.end());
  outer.erase(unique(outer.begin(), outer.end()), outer.end());
  if (Xsum != (int)outer.size())
  {
    cout << "No" << endl;
    return;
  }
  
  vector<int> P(6);
  iota(P.begin(), P.end(), 0);
  
  bool res = false;
  do
  {
    if (P[0] != 0) break;
    vector<vector<vector<int>>> copy = blocks;
    for (int i = 0; i < 6; ++i)
    {
      copy[i] = blocks[P[i]];
    }
    
    for (int rev = 0; rev < 2; ++rev)
    {
      res |= dfs(1, copy, cover);
      rever(copy[0]);
    }
    if (res) break;
    
  } while (next_permutation(P.begin(), P.end()));
  
  cout << (res ? "Yes" : "No") << endl;
  
  return;
}

int main()
{
  while (!is_end)
  {
    solve();
  }
  
  return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 3376
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for(int i = a; i < n; i++)
#define rrep(i, a, n) for(int i = a; i >= n; i--)
#define inr(l, x, r) (l <= x && x < r)
#define ll long long
#define ld long double

constexpr int IINF = 1001001001;
constexpr ll INF = 9e18;

template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}

int main(){

    const int m = 6;
    auto calc_triple = [&](vector<pair<int, int>> p, int num, int width) -> vector<tuple<int, int, int>> {
        vector<tuple<int, int, int>> res(p.size());
        int n = width;
        rep(i, 0, p.size()){
            auto [r, c] = p[i];
            if(num == 0){
                // x = 0
                res[i] = {0, r, c};
            }else if(num == 1){
                // x = n-1
                res[i] = {n-1, r, c};
            }else if(num == 2){
                // y = 0
                res[i] = {r, 0, c};
            }else if(num == 3){
                // y = n-1
                res[i] = {r, n-1, c};
            }else if(num == 4){
                // z = 0
                res[i] = {n-1-c, r, 0};
            }else if(num == 5){
                // z = n-1
                res[i] = {n-1-c, r, n-1};
            }
        }
        return res;
    };
    while(true){
        int n; cin >> n;
        if(n == 0) break;

        vector<vector<pair<int, int>>> p(m);
        rep(i, 0, m){
            rep(j, 0, n){
                string s; cin >> s;
                rep(k, 0, n){
                    if(s[k] == 'X'){
                        p[i].push_back({n-1-j, k});
                    }
                }
            }
        }

        // vector<vector<vector<tuple<int, int, int>>>> t(m, vector<vector<tuple<int, int, int>>>(m));
        // rep(i, 0, m){
        //     rep(j, 0, m){
        //         t[i][j] = calc_triple(p[i], j, n);
        //     }
        // }

        vector<int> q(m);
        iota(q.begin(), q.end(), 0);
        

        bool ans = false;
        vector<vector<vector<bool>>> ok(n, vector<vector<bool>>(n, vector<bool>(n)));
        auto f = [&](auto self, int pos, vector<int> q_) -> bool {
            if(pos == m){
                bool flag = true;
                rep(i, 0, n){
                    rep(j, 0, n){
                        if(!ok[i][j][0]) flag = false;
                        if(!ok[i][j][n-1]) flag = false;
                        if(!ok[i][0][j]) flag = false;
                        if(!ok[i][n-1][j]) flag = false;
                        if(!ok[0][i][j]) flag = false;
                        if(!ok[n-1][i][j]) flag = false;
                        if(!flag) break;
                    }
                    if(!flag) break;
                }
                return flag;
            }
            bool res = false;
            rep(_, 0, 2){
                rep(__, 0, 4){
                    bool flag = true;
                    vector<tuple<int, int, int>> t = calc_triple(p[pos], q[pos], n);
                    rep(i, 0, t.size()){
                        auto [x, y, z] = t[i];
                        if(ok[x][y][z]){
                            flag = false;
                            break;
                        }
                    }
                    if(flag){
                        rep(i, 0, t.size()){
                            auto [x, y, z] = t[i];
                            ok[x][y][z] = 1;
                        }
                        res |= self(self, pos+1, q_);
                        if(res) return true;
                        rep(i, 0, t.size()){
                            auto [x, y, z] = t[i];
                            ok[x][y][z] = 0;
                        }
                    }
                    rep(i, 0, p[pos].size()){
                        p[pos][i] = {n-1-p[pos][i].second, p[pos][i].first};
                    }
                }
                rep(i, 0, p[pos].size()){
                    p[pos][i] = {n-1-p[pos][i].first, p[pos][i].second};
                }
            }
            return res;
        };
        do{
            if(q[0] != 0) break;

            ans |= f(f, 0, q);
            if(ans) break;
        }while(next_permutation(q.begin(), q.end()));
        if(!ans) cout << "No" << endl;
        else cout << "Yes" << endl;
    }
    
    return 0;
}
accuracy: 1
time_ms: 6120
memory_kb: 3244
final_rank: 17

code_snippet: // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i, n) for (long long i = 0; i < (n); i++)
#define rep1(i, n) for (long long i = 1; i <= (n); i++)
#define all(v) v.begin(), v.end()
#define decimal(n) cout << fixed << setprecision(n);
using namespace std;
// using namespace atcoder;
using vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>;
using ll = long long; using vl = vector<ll>; using vvl = vector<vl>; 
// using mint = modint998244353;
const ll MOD = 998244353;
// using mint = modint;
// const ll MOD = 998244353;
// using mint = modint1000000007;
// const ll MOD = 1000000007;
// using vm = vector<mint>; using vvm = vector<vm>;
using vll = vl;
using P = pair<ll, ll>; using T = tuple<ll, ll, ll>;
using vc = vector<char>; using vvc = vector<vc>;
const ll MAX = 2000100; const ll llINF = 2e18;
const int intINF = 2e9; const long double pi = acos(-1);
ll di[] = {0, 0, -1, 0, 1, -1, -1, 1, 1};
ll dj[] = {0, 1, 0, -1, 0, 1, -1, -1, 1};
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

ll n;
vector<P> ijpairs;

void rotate(vs &piece) {
    vs next(n);
    rep(i, n) {
        next[i] = string(n, 'X');
    }
    for(auto [i, j] : ijpairs) {
        next[j][n - 1 - i] = piece[i][j];
    }
    swap(piece, next);
}

void flip(vs &piece) {
    vs next(n);
    rep(i, n) {
        next[i] = string(n, 'X');
    }
    for(auto [i, j] : ijpairs) {
        next[n - 1 - i][j] = piece[i][j];
    }
    swap(piece, next);
}

bool pieceset(vs &piece, vector<vs> &cube, ll x, ll y, ll z) {
    if(z != -1) {
        ll k = z;
        for(auto [i, j] : ijpairs) {
            if(piece[i][j] == 'X') {
                if(cube[i][j][k] == 'X') {
                    return false;
                }
                cube[i][j][k] = piece[i][j];
            }
        }
    }
    else if(x != -1) {
        ll i = x;
        for(auto [j, k] : ijpairs) {
            if(piece[j][k] == 'X') {
                if(cube[i][j][k] == 'X') {
                    return false;
                }
                cube[i][j][k] = piece[j][k];
            }
        }
    }
    else {
        ll j = y;
        for(auto [i, k] : ijpairs) {
            if(piece[i][k] == 'X') {
                if(cube[i][j][k] == 'X') {
                    return false;
                }
                cube[i][j][k] = piece[i][k];
            }
        }
    }
    return true;
}

bool solve() {
    ijpairs.clear();
    rep(i, n) {
        rep(j, n) {
            if(i == 0 || i == n - 1 || j == 0 || j == n - 1) {
                ijpairs.emplace_back(make_pair(i, j));
            }
        }
    }

    
    // ピースのうけとりと単位立方体の数カウント
    ll PIECECOUNT = 6;
    vector<vector<string>> masterpieces(PIECECOUNT);
    ll cnt = 0;
    rep(pid, PIECECOUNT) {
        masterpieces[pid].resize(n);
        rep(i, n) cin >> masterpieces[pid][i];
        rep(i, n) {
            rep(j, n) {
                cnt += (masterpieces[pid][i][j] == 'X');
            }
        }
    }
    if(cnt != 2 * n * n + 4 * (n - 2) * (n - 1)) {
        return false;
    }

    rep(baseflip, 2) {
        // キューブの初期化
        vector<vs> mastercube(n, vs(n));
        rep(i, n) {
            rep(j, n) {
                mastercube[i][j] = string(n, '.');
            }
        }

        vector<vector<ll>> corbase = {{-1, 0, -1}, {-1, -1, 0}, {n - 1, -1, -1}, {-1, n - 1, -1}, {-1, -1, n - 1}, {0, -1, -1}};
        if(baseflip) flip(masterpieces.back());
        pieceset(masterpieces.back(), mastercube, 0, -1, -1);

        vl piece_order(PIECECOUNT - 1);
        iota(all(piece_order), 0LL);

        do {
            // ピースを一つずつ当てはめていく
            for(ll statebit = 0; statebit < (1LL << (3 * (PIECECOUNT - 1))); statebit++) {
                vector<vs> cube = mastercube;
                bool flag = true;
                rep(piece_id, PIECECOUNT - 1) {
                    auto piece = masterpieces[piece_order[piece_id]];
                    ll shiftbit = 3 * (PIECECOUNT - 2 - piece_id);

                    // 回転
                    ll nowstate = (statebit >> shiftbit); 
                    ll rottime = nowstate & 3;
                    rep(t, rottime) {
                        rotate(piece);
                    }

                    // 反転
                    if(nowstate & 4) {
                        flip(piece);
                    }

                    // セットして確認
                    ll x = corbase[piece_id][0];
                    ll y = corbase[piece_id][1];
                    ll z = corbase[piece_id][2];
                    if(!pieceset(piece, cube, x, y, z)) {
                        flag = false;
                        statebit = ((statebit >> shiftbit) << shiftbit) + ((1LL << shiftbit) - 1);
                        break;
                    }
                }
                if(flag) {
                    return true;
                }
            }
        } while(next_permutation(all(piece_order)));
    }
    return false;
}

int main() {
    while(true) {
        cin >> n;
        if(n == 0) break;
        cout << (solve() ? "Yes" : "No") << endl;
    }
}
accuracy: 1
time_ms: 1630
memory_kb: 3352
final_rank: 7

code_snippet: // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i, n) for (long long i = 0; i < (n); i++)
#define rep1(i, n) for (long long i = 1; i <= (n); i++)
#define all(v) v.begin(), v.end()
#define decimal(n) cout << fixed << setprecision(n);
using namespace std;
// using namespace atcoder;
using vi = vector<int>; using vvi = vector<vi>; using vs = vector<string>;
using ll = long long; using vl = vector<ll>; using vvl = vector<vl>; 
// using mint = modint998244353;
const ll MOD = 998244353;
// using mint = modint;
// const ll MOD = 998244353;
// using mint = modint1000000007;
// const ll MOD = 1000000007;
// using vm = vector<mint>; using vvm = vector<vm>;
using vll = vl;
using P = pair<ll, ll>; using T = tuple<ll, ll, ll>;
using vc = vector<char>; using vvc = vector<vc>;
const ll MAX = 2000100; const ll llINF = 2e18;
const int intINF = 2e9; const long double pi = acos(-1);
ll di[] = {0, 0, -1, 0, 1, -1, -1, 1, 1};
ll dj[] = {0, 1, 0, -1, 0, 1, -1, -1, 1};
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

ll n;
vector<P> ijpairs;

void rotate(vs &piece) {
    vs next(n);
    rep(i, n) {
        next[i] = string(n, 'X');
    }
    for(auto [i, j] : ijpairs) {
        next[j][n - 1 - i] = piece[i][j];
    }
    swap(piece, next);
}

void flip(vs &piece) {
    vs next(n);
    rep(i, n) {
        next[i] = string(n, 'X');
    }
    for(auto [i, j] : ijpairs) {
        next[n - 1 - i][j] = piece[i][j];
    }
    swap(piece, next);
}

bool pieceset(vs &piece, vector<vs> &cube, ll x, ll y, ll z) {
    if(z != -1) {
        ll k = z;
        for(auto [i, j] : ijpairs) {
            if(piece[i][j] == 'X') {
                if(cube[i][j][k] == 'X') {
                    return false;
                }
                cube[i][j][k] = piece[i][j];
            }
        }
    }
    else if(x != -1) {
        ll i = x;
        for(auto [j, k] : ijpairs) {
            if(piece[j][k] == 'X') {
                if(cube[i][j][k] == 'X') {
                    return false;
                }
                cube[i][j][k] = piece[j][k];
            }
        }
    }
    else {
        ll j = y;
        for(auto [i, k] : ijpairs) {
            if(piece[i][k] == 'X') {
                if(cube[i][j][k] == 'X') {
                    return false;
                }
                cube[i][j][k] = piece[i][k];
            }
        }
    }
    return true;
}

bool solve() {
    ijpairs.clear();
    rep(i, n) {
        rep(j, n) {
            if(i == 0 || i == n - 1 || j == 0 || j == n - 1) {
                ijpairs.emplace_back(make_pair(i, j));
            }
        }
    }

    
    // ピースのうけとりと単位立方体の数カウント
    ll PIECECOUNT = 6;
    vector<vector<string>> masterpieces(PIECECOUNT);
    ll cnt = 0;
    rep(pid, PIECECOUNT) {
        masterpieces[pid].resize(n);
        rep(i, n) cin >> masterpieces[pid][i];
        rep(i, n) {
            rep(j, n) {
                cnt += (masterpieces[pid][i][j] == 'X');
            }
        }
    }
    if(cnt != 2 * n * n + 4 * (n - 2) * (n - 1)) {
        return false;
    }

    // キューブの初期化
    vector<vs> mastercube(n, vs(n));
    rep(i, n) {
        rep(j, n) {
            mastercube[i][j] = string(n, '.');
        }
    }

    vector<vector<ll>> corbase = {{-1, 0, -1}, {-1, -1, 0}, {n - 1, -1, -1}, {-1, n - 1, -1}, {-1, -1, n - 1}, {0, -1, -1}};
    pieceset(masterpieces.back(), mastercube, 0, -1, -1);

    vl piece_order(PIECECOUNT - 1);
    iota(all(piece_order), 0LL);

    do {
        // ピースを一つずつ当てはめていく
        for(ll statebit = 0; statebit < (1LL << (3 * (PIECECOUNT - 1))); statebit++) {
            vector<vs> cube = mastercube;
            bool flag = true;
            rep(piece_id, PIECECOUNT - 1) {
                auto piece = masterpieces[piece_order[piece_id]];
                ll shiftbit = 3 * (PIECECOUNT - 2 - piece_id);

                // 回転
                ll nowstate = (statebit >> shiftbit); 
                ll rottime = nowstate & 3;
                rep(t, rottime) {
                    rotate(piece);
                }

                // 反転
                if(nowstate & 4) {
                    flip(piece);
                }

                // セットして確認
                ll x = corbase[piece_id][0];
                ll y = corbase[piece_id][1];
                ll z = corbase[piece_id][2];
                if(!pieceset(piece, cube, x, y, z)) {
                    flag = false;
                    statebit = ((statebit >> shiftbit) << shiftbit) + ((1LL << shiftbit) - 1);
                    break;
                }
            }
            if(flag) {
                return true;
            }
        }
    } while(next_permutation(all(piece_order)));

    // ベースのピースをflipしてもう一回
    rep(i, n) {
        rep(j, n) {
            mastercube[i][j] = string(n, '.');
        }
    }
    flip(masterpieces.back());
    pieceset(masterpieces.back(), mastercube, 0, -1, -1);


    iota(all(piece_order), 0LL);

    do {
        // ピースを一つずつ当てはめていく
        for(ll statebit = 0; statebit < (1LL << (3 * (PIECECOUNT - 1))); statebit++) {
            vector<vs> cube = mastercube;
            bool flag = true;
            rep(piece_id, PIECECOUNT - 1) {
                auto piece = masterpieces[piece_order[piece_id]];
                ll shiftbit = 3 * (PIECECOUNT - 2 - piece_id);

                // 回転
                ll nowstate = (statebit >> shiftbit); 
                ll rottime = nowstate & 3;
                rep(t, rottime) {
                    rotate(piece);
                }

                // 反転
                if(nowstate & 4) {
                    flip(piece);
                }

                // セットして確認
                ll x = corbase[piece_id][0];
                ll y = corbase[piece_id][1];
                ll z = corbase[piece_id][2];
                if(!pieceset(piece, cube, x, y, z)) {
                    flag = false;
                    statebit = ((statebit >> shiftbit) << shiftbit) + ((1LL << shiftbit) - 1);
                    break;
                }
            }
            if(flag) {
                return true;
            }
        }
    } while(next_permutation(all(piece_order)));

    return false;
}

int main() {
    while(true) {
        cin >> n;
        if(n == 0) break;
        cout << (solve() ? "Yes" : "No") << endl;
    }
}
accuracy: 1
time_ms: 1590
memory_kb: 3408
final_rank: 10