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

const int N = 20;

int n;
mt19937 mt;

bool check(string s, vector<vector<char>>& a, int i, int j) {
    bool isOk = true;
    for (int k = 0; k < s.size() && isOk; ++k) {
        if (s[k] != a[i][(j + k) % n]) {
            isOk = false;
        }
    }
    if (isOk) {
        return true;
    }
    for (int k = 0; k < s.size() && isOk; ++k) {
        if (s[k] != a[(i + k) % n][j]) {
            isOk = false;
        }
    }
    return isOk;
}

int calc(vector<string>& v, vector<vector<char>>& a) {
    int cnt = 0;
    for (string& s : v) {
        bool found = false;
        for (int i = 0; i < n && !found; ++i) {
            for (int j = 0; j < n && !found; ++j) {
                if (check(s, a, i, j)) {
                    found = true;
                }
            }
        }
        cnt += found;
    }
    return cnt;
}

pair<int, vector<vector<char>>> gen(vector<string>& v) {
    shuffle(v.begin(), v.end(), mt);
    vector<vector<char>> a(n, vector<char>(n, 'A'));
    /*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];
        }
    }
    return {calc(v, a), a};
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    mt.seed(time(nullptr));
    int m;
    cin >> n >> m;
    vector<string> v(m);
    for (auto& i : v) {
        cin >> i;
    }
    pair<int, vector<vector<char>>> res = gen(v);
    for (int i = 0; i < 400; ++i) {
        auto curr = gen(v);
        if (curr.first > res.first) {
            res = curr;
        }
    }
    for (auto& i : res.second) {
        for (auto& j : i) {
            cout << j;
        }
        cout << '\n';
    }
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (decltype(n) i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (decltype(n) i = 1, i##_len = (n); i <= i##_len; ++i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define pl(s) cout << (s) << "\n";
#define plx(s) {cout << (s) << "\n"; exit(0);}
#ifdef __LOCAL
#include <dump.hpp>
#define dump(...) DUMPOUT << "  " << string(#__VA_ARGS__) << ": " << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl << "    ", dump_func(__VA_ARGS__)
#else
#define dump(...)
#endif
using ll = long long; using ld = long double; template<class T> using V = vector<T>;
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> istream &operator>>(istream &is, complex<T> &v) {T x, y; is >> x >> y; v.real(x); v.imag(y); return is;}
template<class T> istream &operator>>(istream &is, V<T> &v) {for (auto&& e : v) is >> e;return is;}
template<class T, size_t n> istream &operator>>(istream &is, array<T, n> &v) {for (auto&& e : v) is >> e;return is;}
template<class T> inline string join(const T& v, string sep = " ") {if (v.size() == 0) return "" ;stringstream ss;for (auto&& e : v) ss << sep << e;return ss.str().substr(1);}
constexpr ll TEN(int n) {return (n == 0) ? 1 : 10 * TEN(n - 1);}
struct IOS {IOS() {cin.tie(nullptr); ios::sync_with_stdio(false); dump("");}} IO;
constexpr int INF = (1 << 30) - 1; constexpr ll INFL = 1LL << 60;

const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

struct Solver {
  V<string> in, out;
  ll output_n, input_m;

  bool isSubString(string &b) {
    rep(i,output_n) rep(j,output_n) rep(d,2) {
      bool ok = true;
      rep(k,sz(b)) {
        ll ni = (i+dy[d]*k) % output_n;
        ll nj = (j+dx[d]*k) % output_n;
        if (out[ni][nj] != b[k]) {
          ok = false;
          break;
        }
      }
      if (ok) return true;
    }
    return false;
  }

  ld computeScore() {
    ll used_count = 0;
    ll space_count = 0;
    rep(i,output_n) rep(j,output_n) if (out[i][j] == '.') ++space_count;
    rep(k,input_m) if (isSubString(in[k])) ++used_count;
    ld score = 0;
    if (used_count < input_m) {
      score = (ld)(TEN(8) * used_count) / (ld)(input_m);
    } else {
      score = (ld)(TEN(8) * 2 * output_n * output_n) / (ld)(2 * output_n * output_n - space_count);
    }
    dump(used_count,input_m,score);
    return round(score);
  }

  void solve() {
    cin >> output_n >> input_m;
    in.resize(input_m); out.resize(output_n);
    cin >> in;
    V<pair<int,string>> in_for_sort(input_m);
    rep(i,input_m) in_for_sort[i] = {sz(in[i]), in[i]};
    sort(rall(in_for_sort));
    string tmp = "";
    rep(i,input_m) tmp += in_for_sort[i].second;
    rep(i,output_n) out[i] = tmp.substr(i*output_n, output_n);
    ll score = computeScore();
    dump(score);
    pl(join(out,"\n"));
  }
} solver;
signed main(void) {solver.solve();return 0;}