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

mt19937 rand_mt(42);

const int N = 20;

class Genome{
public:
    Genome(const vector<string>& S): sequences(S){

        // auto dna = init_random();
        auto dna = init_bf();

        for(int i=0; i<N; i++){
            dna_w[i] = dna[i];

            for(int j=0; j<N; j++){
                dna_h[j] += dna[i][j];
            }
        }
        for(int i=0; i<N; i++){
            dna_w[i] += dna_w[i];
            dna_h[i] += dna_h[i];
        }
    }

    array<string, N> init_random(){
        auto dna = array<string, N>();
        for(int i=0; i<N; i++){
            for(int j=0; j<N; j++){
                dna[i] += 'A' + rand_mt() % 8;
            }
        }
        return dna;
    }

    array<string, N> init_bf(){
        auto dna = array<string, N>();
        int i = 0;
        for(auto s : sequences){
            for(int i=0; i<N; i++){
                if(N - dna[i].size() < s.size()) continue;
                
                dna[i] += s;
                break;
            }
        }
        return dna;
    }

    int evaluate(){
        const auto M = sequences.size();
        int c = 0;
        for(auto s : sequences){
            bool hasSeq = false;
            for(auto dw : dna_w){
                if(dw.find(s) != string::npos){ hasSeq = true; break;}
            }
            if (hasSeq){ c++; continue;}

            for(auto dw : dna_h){
                if(dw.find(s) != string::npos){ hasSeq = true; break;}
            }
            if (hasSeq){ c++; continue;}
        }

        int d = 0;
        auto dna = getDNA();
        for(auto s : dna){
            d += count(s.begin(), s.end(), '.');
        }

        if(c < M){
            return int(1e+8 * (c / (double) M));
        }else{
            return int(1e+8 * ((2 * N * N) / (double) (2 * N * N - d)));
        }
    }

    array<string, N> getDNA(){
        auto dna = array<string, N>();
        for(int i=0; i<N; i++){
            dna[i] = dna_w[i].substr(0, 20);
        }
        return dna;
    }

    void print(){
        auto dna = getDNA();
        for(int i=0; i<N; i++){
            cout << dna[i] << endl;
        }
    }


public:
    array<string, N> dna_w;
    array<string, N> dna_h;

    const vector<string> sequences;

};

int main()
{
    int M;
    cin >> M >> M;
    vector<string> S(M, "");
    for(int i=0; i<M; i++) cin >> S[i];
    sort(S.begin(), S.end(), [](const string& s1, const string& s2){return s1.size() > s2.size();});

    auto genome = Genome(S);
    genome.print();

    // cerr << "score = "<< genome.evaluate() << endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define ll long long
#define ln cout<<endl
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1};
typedef pair<ll,ll> P;

vector<string> C(vector<string> s) {
  ll n=s.size();
  rep(i,n)REP(j,i+1,n) swap(s[i][j],s[j][i]);
  return s;
}

ll n,m;
string s[1000];

ll calc(vector<string> t) {
  ll cnt=0;
  rep(k,m) {
    ll f=0;
    rep(l,2) {
      rep(i,n) {
        rep(j,n) {
          if(t[i].substr(0,s[k].size())==s[k]) f=1;
          rotate(t[i].begin(),t[i].begin()+1,t[i].end());
        }
      }
      t=C(t);
    }
    cnt+=f;
  }
  return cnt;
}

vector<string> ans;
ll MM;

void solve(vector<string> t) {
  ll d=calc(t);
  if(MM<d) {
    MM=d;
    ans=t;
  }
  rep(k,5) {
    random_shuffle(all(t));
    rep(i,n) rotate(t[i].begin(),t[i].begin()+rand()%n,t[i].end());
    ll d=calc(t);
    if(MM<d) {
      MM=d;
      ans=t;
    }
  }
}

void Main() {
  srand((unsigned)time(NULL));
  cin >> n >> m;
  rep(i,m) R s[i];
  vector<string> v[15];
  rep(i,m) v[s[i].size()].pb(s[i]);
  rep(e,30) {
    vector<string> t(n);
    rrep(i,15) {
      random_shuffle(all(v[i]));
      rep(j,v[i].size()) {
        ll M=MAX,x=0;
        string rr;
        rep(k,n) {
          rep(l,t[k].size()+1) {
            string r1=t[k].substr(l);
            if(r1.size()<=v[i][j].size()) {
              string r2=v[i][j].substr(0,r1.size());
              if(r1==r2) {
                ll d=t[k].size()+v[i][j].size()-r1.size();
                if(d<=n&&M>d) {
                  M=d;
                  x=k;
                  rr=t[k]+v[i][j].substr(r1.size());
                }
              }
            }
          }
        }
        if(rand()%9==0) M=MAX;
        if(M!=MAX) t[x]=rr;
      }
    }
    rep(i,n) {
      while(t[i].size()<n) {
        //t[i]+='.';
        t[i]+=(char)(rand()%8+'A');
      }
    }
    solve(t);
  }
  rep(i,n) pr(ans[i]);
}

int main(){Main();return 0;}
