#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)

using namespace std;
using LL = long long;
using P = pair<int,string>;
using vv = vector<vector<int>>;
const int INF = (int)1e9;
const LL LINF = (LL)1e18;
const LL sc = (int)1e8;
const double TL = 2.85;
const int Max_N = 20;
const int Max_K = 8;
const int Max_length = 12;
const double blank_poss = 1.00;

int N, M;
vector<P> s;
vector<vector<char>> mat;
map<string,int> mp;
LL score(){
	int blank = 0, hit = 0;
	rep(i,N){
		rep(j,N) if(mat[i][j] == '.') blank++;
	}
	rep(i,M) if(mp[s[i].second] > 0) hit++;
	LL res = sc;
	if(hit < M){
		res *= hit;
		res /= M;
	}
	else{
		res *= N * N * 2;
		res /= (N * N * 2 - blank);
	}
	return res;
}
void del(int h, int w){
	for(int l = 2; l <= Max_length; l++){
		rep(i,l){
			string t1, t2;
			int st = h - i;
			if(st < 0) st += N;
			rep(j,l) t1 += mat[(st+j)%N][w];
			st = w - i;
			if(st < 0) st += N;
			rep(j,l) t2 += mat[h][(st+j)%N];
			mp[t1]--;
			mp[t2]--;
		}
	}
}
void add(int h, int w, char c){
	mat[h][w] = c;
	for(int l = 2; l <= Max_length; l++){
		rep(i,l){
			string t1, t2;
			int st = h - i;
			if(st < 0) st += N;
			rep(j,l) t1 += mat[(st+j)%N][w];
			st = w - i;
			if(st < 0) st += N;
			rep(j,l) t2 += mat[h][(st+j)%N];
			mp[t1]++;
			mp[t2]++;
		}
	}
}

int main(){
	//FILE *outputfile;
	//outputfile = freopen("test.txt", "w", stdout);

	random_device rnd;
	mt19937 mt(rnd());
	uniform_int_distribution<> pos(0, Max_N - 1);
	uniform_int_distribution<> code(0, Max_K - 1);
	uniform_real_distribution<> R(0.0, 1.0);
	cin >> N >> M;
	s.resize(M);
	rep(i,M){
		string t;
		cin >> t;
		s[i] = P((int)t.size(), t);
	}
	sort(s.begin(), s.end());
	mat.resize(N);
	rep(i,N){
		rep(j,N){
			int k = code(mt);
			char c = 'A' + k;
			double r = R(mt);
			if(r < blank_poss) mat[i].push_back('.');
			else mat[i].push_back(c);
		}
	}
	int now_h = 0, now_w = 0;
	rep(i,M){
		string t = s[i].second;
		int siz = t.size();
		if(now_w + siz > N){
			if(now_h == N - 1) break;
			else{
				now_h++;
				now_w = 0;
			}
		}
		rep(j,siz) add(now_h, now_w + j, t[j]);
		now_w += siz;
	}
	double T0 = 1e5, T1 = 5e2;
	while(1.0 * clock() / CLOCKS_PER_SEC < TL){
		double t = 1.0 * clock() / CLOCKS_PER_SEC / TL;
		double temp = pow(T0, 1.0 - t) * pow(T1, t);
		LL now_score = score();
		int h = pos(mt), w = pos(mt);
		int k = code(mt);
		char new_c = 'A' + k;
		char old_c = mat[h][w];
		del(h, w);
		add(h, w, new_c);
		LL new_score = score();
		LL dif = new_score - now_score;
		if(dif < 0){
			double change_poss = exp(dif / temp);
			if(change_poss < R(mt)){
				del(h, w);
				add(h, w, old_c);
			}
		}
	}
	vector<string> ans(N);
	rep(i,N){
		rep(j,N) ans[i].push_back(mat[i][j]);
	}
	rep(i,N) cout << ans[i] << endl;
	//LL fin = score();
	//cout << fin << endl;

	return 0;
}

//GIVE ME AC!!!!!!!!!!!!!!!!!
//#pragma GCC target("avx")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define floatset() fixed<<setprecision(15)
#define all(n) n.begin(),n.end()
#define rall(n) n.rbegin(),n.rend()
#define rep(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define pb push_back
#define eb emplace_back
#define INT(...) int __VA_ARGS__;scan(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;scan(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;scan(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;scan(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;scan(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;scan(__VA_ARGS__)
using namespace std;
using vl=vector<ll>;
using vi=vector<int>;
using vs=vector<string>;
using vc=vector<char>;
using vvl=vector<vl>;
using P=pair<ll,ll>;
using vvc=vector<vc>;
using vd=vector<double>;
using vp=vector<P>;
using vb=vector<bool>;
using P=pair<ll,ll>;
const int dx[8]={1,0,-1,0,1,-1,-1,1};
const int dy[8]={0,1,0,-1,1,1,-1,-1};
const ll inf =1e18;
const ll MOD=1000000007;
const ll mod=998244353;
const double pi=acos(-1);
template<typename T1,typename T2 >
ostream &operator<<(ostream&os,const pair<T1,T2>&p) {
  os<<p.first<<" "<<p.second;
  return os;
}
template<typename T1,typename T2>
istream &operator>>(istream&is,pair<T1,T2>&p) {
  is>>p.first>>p.second;
  return is;
}
template<typename T>
ostream &operator<<(ostream&os,const vector<T>&v) {
  for(int i=0;i<(int)v.size();i++) {
    os<<v[i]<<(i+1!=v.size()?" ":"");
  }
  return os;
}
template<typename T>
istream &operator>>(istream&is,vector<T>&v) {
  for(T &in:v)is>>in;
  return is;
}
void scan(){}
template<class Head,class... Tail>
void scan(Head&head,Tail&... tail) {
  cin>>head;
  scan(tail...);
}
template<class T>
void print(const T &t) { cout << t << '\n'; }
template<class Head, class... Tail>
void print(const Head &head, const Tail &... tail) {
  cout << head << ' ';
  print(tail...);
}
template<class... T>
void fin(const T &... a) {
  print(a...);
  exit(0);
}
template<typename T1,typename T2>
inline bool chmax(T1&a,T2 b){return a<b&&(a=b,true);}
template<typename T1,typename T2>
inline bool chmin(T1&a,T2 b){return a>b&&(a=b,true);}
random_device seed_gen;
mt19937 engine(seed_gen());
int main(){
  LL(n,m);
  vs a(m);
  cin>>a;
 rep(i,0,100){
  shuffle(all(a),engine);
 }
  ll now=0;
  rep(i,0,n){
    ll cnt=0;
    string ans="";
    while(cnt+(ll)(a[now].size())<=n){
      cnt+=a[now].size();
      ans+=a[now];
      now++;
    }
    rep(i,0,n-cnt){
      ll x=engine();
      x%=8;
      cout<<(char)('A'+x);
    }
    cout<<ans<<endl;
  }
}