#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,35) {
    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(l+v[i][j].size()<=t[k].size()) {
              r1=t[k].substr(l,v[i][j].size());
              if(r1==v[i][j]) {
                M=0;
                x=k;
                rr=t[k];
              }
            }
          }
        }
        if(rand()%(i+3)==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;}

#include<iostream>
#include<cstring>
using namespace std;
const int BUF = 73;


int nPrime;
int prime[BUF];

void makePrime() {
    bool isPrime[BUF] = {};
    for (int i = 0; i < BUF; ++i) isPrime[i] = true;

    for (int i = 2; i * i < BUF; ++i) {
        if (isPrime[i]) {
            for (int j = i * 2; j < BUF; j += i) {
                isPrime[j] = false;
            }
        }
    }

    nPrime = 0;
    for (int i = 2; i < BUF; ++i) {
        if (isPrime[i]) {
            prime[nPrime++] = i;
        }
    }
}


long long A, B;

void read() {
    cin >> A >> B;
}


long long rec(int idx, int mask, long long dp[BUF][1 << 20]) {
    if (idx == B - A + 1) return 1;
    
    long long &ret = dp[idx][mask];
    if (ret != -1) return ret;

    ret = 0;
    
    long long val = A + idx;

    // Pick
    int nexMask = mask;
    for (int i = 0; i < nPrime; ++i) {
        if (val % prime[i] == 0) {
            if (nexMask & (1 << i)) goto _fail;
            nexMask |= 1 << i;
        }
    }

    ret += rec(idx + 1, nexMask, dp);
    
 _fail:
    
    // Don't pick
    ret += rec(idx + 1, mask, dp);
    
    return ret;
}


void work() {
    static long long dp[BUF][1 << 20];
    memset(dp, -1, sizeof(dp));
    cout << rec(0, 0, dp) << endl;
}


int main() {
    makePrime();
    read();
    work();
    return 0;
}
