#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
typedef int64_t lint;
#define rep(i, n) for(int i=0; i<n; i++)
#define repx(i, l, n) for(int i=l; i<n; i++)
#define all(v) v.begin(), v.end()
#define show(x) cout << #x << ": " << x << endl;
#define list(x) cout << #x << ": " << x << "  ";
#define pb push_back
using vi = vector<lint>;
using vvi = vector<vector<lint>>;
template<class T> inline void vin(vector<T>& v) { rep(i, v.size()) cin >> v.at(i); }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> inline void drop(T x) { cout << x << endl; exit(0); }
template<class T> void vout(vector<T> v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; }
constexpr lint LINF = LLONG_MAX/2;

int N, M, TIME=clock();
vector<pair<int, string>> V;
vi C(8);
int sum = 0;
vector<string> S(20);

int total() {
	string s;
	int p = 0;
	set<string> t, u;
	rep(i, M) t.insert(V[i].second);
	vector<string> v(20);
	rep(i, N) v[i] = S[i];
	rep(q, 2) {
		rep(i, N) {
			s = v[i] + v[i];
			for (auto a : t) {
				bool f = false;
				rep(j, N) {
					if (a == s.substr(j, a.size())) {
						f = true;
						break;
					}
				}
				if (f) p += a.size();
				else u.insert(a);
			}
			t.clear();
			swap(t, u);
		}
		rep(i, N) { v[i].clear(); rep(j, N) v[i] += S[j][i]; }
	}
	return p;
}

int delta(int a, int b, int x, int y) {
	string s;
	int p = 0;
	set<string> t, u;
	rep(i, M) t.insert(V[i].second);
	rep(q, 2) {
		s = S[a] + S[a];
		for (auto a : t) {
			bool f = false;
			int c = (b+N-a.size()+1)%N;
			rep(j, a.size()) {
				if (a == s.substr(c+j, a.size())) {
					f = true;
					break;
				}
			}
			if (f) p += a.size();
			else u.insert(a);
		}
		t.clear();
		swap(t, u);
		swap(a, x);
	}
	rep(q, 2) {
		s = "";
		rep(i, N) s += S[i][b];
		rep(i, N) s += S[i][b];
		for (auto a : t) {
			bool f = false;
			int c = (b+N-a.size()+1)%N;
			rep(j, a.size()) {
				if (a == s.substr(c+j, a.size())) {
					f = true;
					break;
				}
			}
			if (f) p += a.size();
			else u.insert(a);
		}
		t.clear();
		swap(t, u);
		swap(b, y);
	}
	return p;
}

int delta_1(int a, int b) {
	string s;
	int p = 0;
	set<string> t, u;
	rep(i, M) t.insert(V[i].second);
	s = S[a] + S[a];
	for (auto a : t) {
		bool f = false;
		int c = (b+N-a.size()+1)%N;
		rep(j, a.size()) {
			if (a == s.substr(c+j, a.size())) {
				f = true;
				break;
			}
		}
		if (f) p += a.size();
		else u.insert(a);
	}
	t.clear();
	swap(t, u);

	s = "";
	rep(i, N) s += S[i][b];
	rep(i, N) s += S[i][b];
	for (auto a : t) {
		bool f = false;
		int c = (b+N-a.size()+1)%N;
		rep(j, a.size()) {
			if (a == s.substr(c+j, a.size())) {
				f = true;
				break;
			}
		}
		if (f) p += a.size();
		else u.insert(a);
	}
	t.clear();
	swap(t, u);
	return p;
}

int main() {
	cin >> N >> M;
	string s;
	rep(i, M) {
		cin >> s;
		V.pb({s.size(), s});
		rep(j, s.size()) C[s[j]-'A']++;
	}
	rep(i, 8) sum += C[i];
	rep(i, 7) C[i+1] += C[i];
	sort(all(V));

	int a=0, b=0, c=0, x, y, z;
	rep(i, N) {
		rep(j, N) {
			x = rand()%sum;
			rep(k, 8) {
				if (x < C[k]) {
					S[i] += 'A'+k;
					break;
				}
			}
		}
	}
	// show(total())
	int count = 0;
	while (true) {
		count++;
		a = rand()%N;
		b = rand()%N;
		if (count%5 == 0) {
			y = S[a][b]-'A';
			x = y;
			while (x == y) x = rand()%8;
			c = delta_1(a, b);
			S[a][b] = 'A'+x;
			z = delta_1(a, b);
			if (c > z) S[a][b] = 'A'+y;
		} else {
			x = rand()%N;
			y = rand()%N;
			c = delta(a, b, x, y);
			swap(S[a][b], S[x][y]);
			z = delta(a, b, x, y);
			if (c > z) swap(S[a][b], S[x][y]);
		}
		// list(c)list(z) show(total())
		if (clock()-TIME > 2.95 * CLOCKS_PER_SEC) break;
	}
	// show(count)
	rep(i, N) std::cout << S[i] << '\n';
}

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

void rotate(string &s , int x) {
    string t = s;
    for (int i = 0; i < s.size(); i++) {
        t[(i + x) % s.size()] = s[i];
    }
    s = t;
    return;
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n , m;
    cin >> n >> m;
    vector<string> sb(m);
    for (int i = 0; i < m; i++) {
        cin >> sb[i];
    }

    for (int i = 0; i < m - 1; i++) {
        for (int j = i + 1; j < m; j++) {
            if (sb[i].find(sb[j]) != -1) {
                sb[j] = "nothing";
            }
        }
    }

    vector<pair<int,string> > s;

    for (int i = 0; i < m; i++) {
        if (sb[i] == "nothing") continue;
        s.push_back(make_pair(sb[i].size() , sb[i]));
    }

    sort(s.begin() , s.end());
    s.erase(unique(s.begin() , s.end()),s.end());
    reverse(s.begin() , s.end());
    m = s.size();
    
    vector<string> ans(n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            ans[i] += '.';
        }
    }

    const int sz = m/ n;
    vector<vector<string> >  s2(sz);

    for (int i = 0; i < sz; i++) {
        for (int j = i * n; j < (i + 1) * n; j++) {
            s2[i].push_back(s[j].second);
        }
    }

    vector<vector<bool> > used(sz , vector<bool>(n , false));

    for (int i = 0; i < sz; i++) {
        for (int j = 0; j < s2[i].size(); j++) {
            string t = s2[i][j];
            bool ok = false;
            string t2 = t;
            int id = 0;
            for (int k = 0; k < ans[j].size(); k++) {
                if (ans[j][k] != '.' && id < t2.size()) {
                    if (ans[j][k] == t2[id]) {
                        id++;
                        if (id == t2.size()) {
                            ok = true;
                        }
                    }
                }
                if (ans[j][k] == '.') {
                    for (int l = 0; l < t.size(); l++) {
                        if (ok) {
                            used[i][j] = true;
                            break;
                        }
                        if (n - k + 1 < t.size()) {
                            break;
                        } else {
                            ans[j][k + l] = t[l];
                            used[i][j] = true;
                        }
                    }
                    break;
                }
            }
        }
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (ans[i][j] == '.') {
                ans[i][j] = 'A';
            }
        }
    }

    for (int i = 0; i < n; i++) {
        rotate(ans[i] , i);
    }

    for (int i = 0; i < n; i++) {
        cout << ans[i] << endl;
    }
    
    return 0;
}

