#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);
    int ind[10] = {};
    for (auto& i : v) {
        for (auto& j : i) {
            ++ind[j - 'A'];
        }
    }
    char a[N][N];
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            a[i][j] = char(mt() % 8 + int('A'));
        }
    }
    /*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;
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define debug(a) { cerr << #a << ':' << a << endl; }
#define endl '\n'
#define fi first
#define se second
using lint = long long;
using P = pair<int,int>;
template<class T> using V = vector<T>;
template<class T> using priq     = priority_queue<T>;
template<class T> using priq_inv = priority_queue<T, vector<T>, greater<T>>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
template<class T> T ceil(const T &a, const T &b) { return (a+b-1)/b; }
template<class T> bool chmin(T &a, T b) { if(a > b) { a = b; return 1; } return 0; }
template<class T> bool chmax(T &a, T b) { if(a < b) { a = b; return 1; } return 0; }
struct INF { template<class T> operator T() { return numeric_limits<T>::max() / 2; } } INF;

template<class T, class U> istream& operator>>(istream &in, pair<T,U> &p) {
    return in >> p.first >> p.second;
}
template<class T, class U> ostream& operator<<(ostream &out, const pair<T,U> &p) {
    return out << "{ " << p.first << ',' << p.second << " } ";
}
template<class T> istream& operator>>(istream &in, vector<T> &v) {
    for(auto &&e: v) in >> e;
    return in;
}
template<class T> ostream& operator<<(ostream &out, const vector<T> &v) {
    for(const auto &e: v) out << e << ' ';
    return out;
}

/*----------------------------------------------------------------------------------------------------*/

int n, m;
V<string> s;

// fstream out("AHC004_out.txt");

int main() {

    clock_t start = clock();

    cin >> n >> m;
    s.resize(m);
    cin >> s;

    V<P> cnts(m); // pair = (count, index);
    V<V<int>> ins(m);

    rep(i,m) {
        int cnt = 0;
        rep(j,m) {
            bool isin = ( s[i].find(s[j]) != string::npos );
            if(isin) {
                cnt++;
                ins[i].push_back(j);
            }
        }
        cnts[i] = make_pair(cnt,i);
    }
    sort(rall(cnts));

    V<bool> used(m);
    V<string> ans(n);

    rep(i,n) { // 初期解生成
        string str = "";
        rep(j,m) {
            int x = cnts[j].se;
            if(used[x]) continue;
            string plus = s[x];
            for(int k = min(sz(str), sz(s[x])); k >= 0; k--) {
                if( str.substr(sz(str)-k,k) == s[x].substr(0,k) ) {
                    plus = s[x].substr(k,sz(s[x])-k);
                    break;
                }
            }
            if(sz(str) + sz(plus) > n) continue;
            str += plus;
            for(int y: ins[x]) used[y] = true;
        }
        str.resize(n,'.');
        ans[i] = str;
    }

    V<int> v(n); iota(all(v), 0);
    pair<int,V<int>> mx = make_pair(0,v);

    random_device seed_gen;
    mt19937 engine(seed_gen());

    while(clock() - start < 2980000) {
        V<bool> nused = used;
        int cnt = 0;
        rep(j,n) { // yoko
            string ns = "";
            rep(i,n) ns += string(1,ans[v[i]][j]);
            ns += ns;
            rep(j,m) {
                if(nused[j]) continue;
                if( ns.find(s[j]) != string::npos ) {
                    cnt++;
                    nused[j] = true;
                }
            }
        }
        chmax(mx, make_pair(cnt, v));
        shuffle(all(v), engine);
    }

    rep(i,n) {
        cout << ans[mx.se[i]] << endl;
    }
}