#include<bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
#define pow(n,m) powl(n,m)
#define sqrt(n) sqrtl(n)
const ll MAX = 5000000000000000000;
const ll MOD = 0;
random_device rd;
mt19937 mt(rd());
int main(){
    ll N,M;
    cin >> N >> M;
    vector<string> S(M),ans(N),T;
    set<string> ss;
    ll m = MAX;
    for(ll i = 0;i < M;i++) cin >> S[i];
    for(ll i = 0;i < M;i++) ss.insert(S[i]);
    for(ll i = 0;i < M;i++) m = min(m,(ll)S[i].size());
    for(ll t = 0;t < 36000;t++){
        string a = S[mt() % M];
        for(ll i = 0;i < 10;i++){
            string b = S[mt() % M];
            if(a.size() + b.size() <= N){
                a += b;
            }
        }
        while(a.size() < N) a += (char)('A' + (mt() % 8));
        T.emplace_back(a);
    }
    vector<ll> aaaa;
    do{
        for(ll j = 0;j < T.size();j++){
            set<string> c;
            for(ll k = 0;k < N;k++){
                string p = "";
                for(ll l = 0;l < m;l++){
                    p += T[j][(k + l) % N];
                }
                for(ll l = m;l < m + 5;l++){
                    p += T[j][(k + l) % N];
                    if(ss.count(p) != 0) c.insert(p);
                }
            }
            aaaa.emplace_back(c.size());
        }
    } while(false);
    sort(aaaa.begin(),aaaa.end(),greater<ll>());
    for(ll i = 0;i < N;i++){
        ll C = 0;
        string A = "____________________";
        for(ll j = 0;j < T.size();j++){
            set<string> c;
            for(ll k = 0;k < N;k++){
                string p = "";
                for(ll l = 0;l < m;l++){
                    p += T[j][(k + l) % N];
                }
                for(ll l = m;l < m + 5;l++){
                    p += T[j][(k + l) % N];
                    if(ss.count(p) != 0) c.insert(p);
                }
            }
            if(C < c.size()){
                C = c.size();
                A = T[j];
            }
            if(i == 0 && aaaa[900] > c.size()){
                T.erase(T.begin() + j);
                j--;
            }
        }
        for(ll k = 0;k < N;k++){
            string p = "";
            for(ll l = 0;l < m;l++){
                p += A[(k + l) % N];
            }
            for(ll l = m;l < m + 5;l++){
                p += A[(k + l) % N];
                if(ss.count(p) != 0) ss.erase(p);
            }
        }
        ans[i] = A;
    }
    ll C = 0;
    vector<string> ansans = ans,ansansans = ans;
    if(m < 5){
        for(ll t = 0;t < 1000;t++){
            swap(ans[mt() % N],ans[mt() % N]);
            for(ll i = 0;i < N;i++){
                ll qqq = mt() % N;
                for(ll j = 0;j < N;j++) ansans[j][i] = ans[i][(j + qqq) % N];
            }
            set<string> c;
            for(ll i = 0;i < N;i++){
                for(ll k = 0;k < N;k++){
                    string p = "";
                    for(ll l = 0;l < m;l++){
                        p += ansans[i][(k + l) % N];
                    }
                    for(ll l = m;l < m + 5;l++){
                        p += ansans[i][(k + l) % N];
                        if(ss.count(p) != 0) c.insert(p);
                    }
                }
            }
            if(C < c.size()){
                C = c.size();
                ansansans = ansans;
            }
        }
    }
    //cout<<C<<endl;
    for(ll i = 0;i < N;i++) cout << ansansans[i] << endl;
}

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

const int N = 20;

mt19937 mt;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    mt.seed(time(nullptr));
    int n, m;
    cin >> n >> m;
    vector<string> v(m);
    for (auto& i : v) {
        cin >> i;
    }
    //sort(v.begin(), v.end(), [](string a, string b){return a.size() < b.size();});
    shuffle(v.begin(), v.end(), mt);
    char a[N][N];
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            a[i][j] = '.';
        }
    }
    int i = 0, j = 0;
    for (auto s : v) {
        if (j + s.size() >= n) {
            j = 0;
            ++i;
        }
        if (i >= n) {
            break;
        }
        for (int k = 0; k < s.size(); ++k, ++j) {
            a[i][j] = s[k];
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cout << a[i][j];
        }
        cout << '\n';
    }
	return 0;
}
