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

class Timer {
public:
    Timer() { start_time_ = std::chrono::steady_clock::now(); }
    int64_t elapsedMilliSeconds() const {
        auto elapsed = std::chrono::steady_clock::now() - start_time_;
        return std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count();
    }

private:
    std::chrono::steady_clock::time_point start_time_;
};

unsigned int randxor() {
    static unsigned int x = 123456789, y = 362436069, z = 521288629, w = 88675123;
    unsigned int t;
    t = (x ^ (x << 11));
    x = y;
    y = z;
    z = w;
    return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}

struct Position {
    int32_t x, y, h_or_v;
};

int main() {
    int32_t N, M;
    cin >> N >> M;
    vector<string> s(M);
    for (string &i : s) {
        cin >> i;
    }

    Timer timer;

    // 操作
    // (1)まだ載せてないものからランダムに選んで載せる
    // (2)すでに載っているものを取り除く
    vector<string> ans(N, string(N, '.'));

    // 各マスが何個の文字列へ影響しているか
    vector<vector<int32_t>> dependence_num(N, vector<int32_t>(N, 0));

    // 各要素がどこにあるか
    vector<Position> positions(M);

    // まだ載せてないもの
    vector<int32_t> remainder(M);
    iota(remainder.begin(), remainder.end(), 0);

    vector<int32_t> on_board;

    constexpr int64_t TIME_LIMIT = 1800;

    for (int32_t index = 0; index < M; index++) {
        // まだ載せてないものをランダムに選んで載せる
        int32_t best_x = -1;
        int32_t best_y = -1;
        int32_t best_h_or_v = -1;
        int32_t best_overlap_num = -1;
        const string &curr_str = s[remainder[index]];

        for (int32_t x = 0; x < N; x++) {
            for (int32_t y = 0; y < N; y++) {
                for (int32_t h_or_v = 0; h_or_v < 2; h_or_v++) {
                    bool ok = true;
                    int32_t overlap_num = 0;
                    for (int32_t i = 0; i < curr_str.size(); i++) {
                        int32_t curr_x = (h_or_v == 0 ? (x + i) % N : x);
                        int32_t curr_y = (h_or_v == 0 ? y : (y + i) % N);


                        if (ans[curr_y][curr_x] == curr_str[i]) {
                            overlap_num++;
                        }
                        if (ans[curr_y][curr_x] != '.' && ans[curr_y][curr_x] != curr_str[i]) {
                            ok = false;
                            break;
                        }
                    }

                    if (ok && overlap_num > best_overlap_num) {
                        //更新
                        best_x = x;
                        best_y = y;
                        best_h_or_v = h_or_v;
                        best_overlap_num = overlap_num;
                    }
                }
            }
        }

        if (best_x == -1) {
            continue;
        }

        // 書き込む
        for (int32_t i = 0; i < curr_str.size(); i++) {
            int32_t curr_x = (best_h_or_v == 0 ? (best_x + i) % N : best_x);
            int32_t curr_y = (best_h_or_v == 0 ? best_y : (best_y + i) % N);
            ans[curr_y][curr_x] = curr_str[i];
            dependence_num[curr_y][curr_x]++;
        }

        positions[remainder[index]].x = best_x;
        positions[remainder[index]].y = best_y;
        positions[remainder[index]].h_or_v = best_h_or_v;
        on_board.push_back(remainder[index]);

        remainder[index] = remainder.back();
        remainder.pop_back();
    }

    for (int32_t i = 0; i < N; i++) {
        cout << ans[i] << endl;
    }
}
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif

int main()
{
	int n, m;
	cin >> n >> m;
	vector<string> ss(m);
	for (auto &&s : ss)
	{
		cin >> s;
	}

	sort(ss.begin(), ss.end());
	ss.erase(unique(ss.begin(), ss.end()), ss.end());
	m = ss.size();

	sort(ss.begin(), ss.end(), [](auto &l, auto &r)
		 {
			 if (l.size() != r.size())
			 {
				 return l.size() > r.size();
			 }
			 return l < r;
		 });

	vector<string> ans(n, string(n, '.'));

	for (int i = 0, j = 0; i < n; i++)
	{
		int l = 0;
		while (j < m && l + ss[j].size() <= n)
		{
			for (int k = 0; k < ss[j].size(); k++)
			{
				ans[i][l + k] = ss[j][k];
			}
			l += ss[j].size();
			j++;
		}
	}

	for (auto &&x : ans)
	{
		cout << x << endl;
	}

	return 0;
}