#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define int long long
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define __builtin_ctz __builtin_ctzll
#define __builtin_clz __builtin_clzll
#define __builtin_popcount __builtin_popcountll
using namespace std;
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
#define next ____next
#define prev ____prev
#define left ____left
#define hash ____hash
typedef long long ll;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<pair<int, int> > VPII;
typedef vector<pair<ll, ll> > VPLL;
template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third;
bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
for (T& v : vec) { ResizeVec(v, sz); }
}
typedef Triple<int, int, int> TIII;
template<class A, class B, class C>
ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
const int N = 222;
VPII Rotate(VPII cells) {
for (auto& p : cells) {
p = {-p.nd, p.st};
}
sort(ALL(cells));
return cells;
}
VPII MirrorY(VPII cells) {
for (auto& p : cells) {
p = {-p.st, p.nd};
}
sort(ALL(cells));
return cells;
}
VPII ShiftToQ1(VPII cells) {
int mi = N, mj = N;
for (auto p : cells) {
mini(mi, p.st);
mini(mj, p.nd);
}
for (auto& p : cells) {
p.st -= mi;
p.nd -= mj;
}
sort(ALL(cells));
return cells;
}
VPII ShiftTo00(VPII cells) {
int mi = cells[0].st;
int mj = cells[0].nd;
for (auto& p : cells) {
p.st -= mi;
p.nd -= mj;
}
return cells;
}
VPII Unambiguous(VPII cells) {
cells = ShiftToQ1(cells);
vector<VPII> que{cells};
set<VPII> secik{cells};
for (int ii = 0; ii < SZ(que); ii++) {
auto v = que[ii];
VPII cand1 = ShiftToQ1(Rotate(v));
VPII cand2 = ShiftToQ1(MirrorY(v));
vector<VPII> cands{cand1, cand2};
for (auto cand : cands) {
if (secik.count(cand)) { continue; }
secik.insert(cand);
que.PB(cand);
}
}
//debug(orig, *secik.begin());
return *secik.begin();
}
vector<VPII> GenAll(VPII cells) {
cells = ShiftToQ1(cells);
vector<VPII> que{cells};
set<VPII> secik{cells};
for (int ii = 0; ii < SZ(que); ii++) {
auto v = que[ii];
VPII cand1 = ShiftToQ1(Rotate(v));
VPII cand2 = ShiftToQ1(MirrorY(v));
vector<VPII> cands{cand1, cand2};
for (auto cand : cands) {
if (secik.count(cand)) { continue; }
secik.insert(cand);
que.PB(cand);
}
}
return que;
}
// struct Tile {
// VPII cells;
// void UnambiguousSelf() {
//
// }
// };
int h, w;
bool IsInBig(int i, int j) {
return i >= 1 && i <= 2 * h && j >= 1 && j <= 2 * w;
}
int di[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dj[] = {0, 0, 1, -1, 1, -1, 1, -1};
char big_board[N][N];
int vis[N][N];
void DfsBig(int i, int j, VPII& poses) {
poses.PB({i, j});
vis[i][j] = 1;
REP (dir, 4) {
int ni = i + di[dir];
int nj = j + dj[dir];
if (IsInBig(ni, nj) && big_board[ni][nj] == big_board[i][j] && !vis[ni][nj]) {
DfsBig(ni, nj, poses);
}
}
}
vector<PII> cells_order;
bool IsOnBoard(int i, int j) {
return i >= 1 && i <= h && j >= 1 && j <= w;
}
void PrintBoard(vector<vector<int>> board) {
vector<vector<char>> ans(h + 1, vector<char>(w + 1, '.'));
RE (i, h) {
RE (j, w) {
if (ans[i][j] != '.') { continue; }
if (board[i][j] == 0) { continue; }
VPII all_my_cells;
RE (ii, h) {
RE (jj, w) {
if (board[i][j] == board[ii][jj]) {
all_my_cells.PB({ii, jj});
}
}
}
set<char> neis_lets;
for (auto p : all_my_cells) {
REP (dir, 4) {
int ni = p.st + di[dir];
int nj = p.nd + dj[dir];
if (IsOnBoard(ni, nj) && ans[ni][nj] != '.') {
neis_lets.insert(ans[ni][nj]);
}
}
}
char let;
FOR (c, 'A', 'Z') {
if (neis_lets.count(c) == 0) {
let = c;
break;
}
}
for (auto p : all_my_cells) {
ans[p.st][p.nd] = let;
}
}
}
debug("lol", h, w);
RE (i, h) {
RE (j, w) {
cout<<ans[i][j];
}
cout<<endl;
}
fflush(stdout);
//assert(false);
}
void Try(map<VPII, int> cnt_map) {
vector<vector<int>> board(h + 1, VI(w + 1));
int rem_area = h * w;
vector<pair<VPII, int>> cnt(ALL(cnt_map));
sort(ALL(cnt), [&](pair<VPII, int> L, pair<VPII, int> R) { if (SZ(L.st) != SZ(R.st)) { return SZ(L.st) > SZ(R.st); } else { return L < R; }});
VI thres{0, 12, 17, SZ(cnt)};
VI tries{30, 15, 19};
int nxt_ind = 1;
for (auto nxt_cell : cells_order) {
if (rem_area < 0) { break; }
int i = nxt_cell.st;
int j = nxt_cell.nd;
if (board[i][j]) { continue; }
int zero_cnt = 0, bad_pos = 0;
REP (phase, 3) {
int L = thres[phase];
int R = thres[phase + 1];
REP (try_ind, tries[phase]) {
int tile_ind = L + rand() % (R - L);
if (cnt[tile_ind].nd == 0) { zero_cnt++; continue; }
vector<VPII> all_poss = GenAll(cnt[tile_ind].st);
random_shuffle(ALL(all_poss));
for (auto poss : all_poss) {
for (auto shift : poss) {
auto nposs = poss;
for (auto& p : nposs) {
p.st -= shift.st;
p.nd -= shift.nd;
}
int fail = 0;
for (auto cell : nposs) {
int ni = i + cell.st;
int nj = j + cell.nd;
if (!IsOnBoard(ni, nj) || board[ni][nj]) { fail = 1; break; }
}
if (fail) {
bad_pos++;
continue;
}
for (auto cell : nposs) {
int ni = i + cell.st;
int nj = j + cell.nd;
board[ni][nj] = nxt_ind;
}
set<PII> neis;
for (auto cell : nposs) {
int ni = i + cell.st;
int nj = j + cell.nd;
REP (dir, 8) {
int nni = ni + di[dir];
int nnj = nj + dj[dir];
if (IsOnBoard(nni, nnj) && board[nni][nnj] == 0) {
neis.insert({nni, nnj});
}
}
}
// if (neis.empty()) {
// debug(nxt_cell);
// PrintBoard(board);
// }
if (neis.empty()) { assert(rem_area == SZ(nposs)); goto Dupa; }
assert(!neis.empty());
vector<PII> que{*neis.begin()};
map<PII, int> vis_neis;
vis_neis[{que[0].st, que[0].nd}] = 1;
for (int ii = 0; ii < SZ(que); ii++) {
int ci = que[ii].st;
int cj = que[ii].nd;
REP (dir, 4) {
int ni = ci + di[dir];
int nj = cj + dj[dir];
if (neis.count({ni, nj})) {
if (vis_neis[{ni, nj}] == 0) {
vis_neis[{ni, nj}] = 1;
que.PB({ni, nj});
}
}
}
}
if (SZ(que) == SZ(neis)) {
cnt[tile_ind].nd--;
rem_area -= SZ(cnt[tile_ind].st);
goto End;
}
// PrintBoard(board);
// debug(neis);
// debug(que);
for (auto cell : nposs) {
int ni = i + cell.st;
int nj = j + cell.nd;
board[ni][nj] = 0;
}
}
}
}
}
// PrintBoard(board);
// debug("fail", bad_pos, zero_cnt, nxt_cell);
// for (auto p : cnt) {
// debug(p.nd, p.st);
// }
return;
End: ;
nxt_ind++;
}
Dupa: ;
debug("start bt");
debug("we're good");
PrintBoard(board);
exit(0);
}
int32_t main() {
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
cin.tie(0);
//double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;
cin>>h>>w;
RE (i, h) {
RE (j, w) {
cells_order.PB({i, j});
}
}
sort(ALL(cells_order), [&](PII L, PII R) {
int mL = min(L.st + w, L.nd + h), mR = min(R.st + w, R.nd + h);
if (mL != mR) {
return mL < mR;
}
return L < R;
});
//debug(cells_order);
RE (i, 2 * h) {
RE (j, 2 * w) {
cin>>big_board[i][j];
}
}
map<VPII, int> cnt;
int cnt_tiles = 0;
int cnt_cells = 0;
VI cnt_by_sz(7);
RE (i, 2 * h) {
RE (j, 2 * w) {
if (big_board[i][j] == '.') { continue; }
if (vis[i][j]) { continue; }
VPII pos;
DfsBig(i, j, pos);
pos = Unambiguous(pos);
cnt_cells += SZ(pos);
cnt_by_sz[SZ(pos)]++;
cnt_tiles++;
cnt[pos]++;
}
}
// debug(cnt_tiles, h * w);
// debug(cnt);
// debug(SZ(cnt));
// for (auto p : cnt) {
// vector<vector<char>> visu(5, vector<char>(5, '.'));
// for (auto para : p.st) {
// visu[para.st][para.nd] = '#';
// }
// REP (i, 5) {
// REP (j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CaWcobmksIG5qKSAmJiBiaWdfYm9hcmRbbmldW25qXSA9PSBiaWdfYm9hcmRbaV1bal0gJiYgIXZpc1tuaV1bbmpdKSB7CiAgICAgIERmc0JpZyhuaSwgbmosIHBvc2VzKTsKICAgIH0KICB9Cn0KCnZlY3RvcjxQSUk+IGNlbGxzX29yZGVyOwpib29sIElzT25Cb2FyZChpbnQgaSwgaW50IGopIHsKICByZXR1cm4gaSA+PSAxICYmIGkgPD0gaCAmJiBqID49IDEgJiYgaiA8PSB3Owp9Cgp2b2lkIFByaW50Qm9hcmQodmVjdG9yPHZlY3RvcjxpbnQ+PiBib2FyZCkgewogIHZlY3Rvcjx2ZWN0b3I8Y2hhcj4+IGFucyhoICsgMSwgdmVjdG9yPGNoYXI+KHcgKyAxLCAnLicpKTsKICBSRSAoaSwgaCkgewogICAgUkUgKGosIHcpIHsKICAgICAgaWYgKGFuc1tpXVtqXSAhPSAnLicpIHsgY29udGludWU7IH0KICAgICAgaWYgKGJvYXJkW2ldW2pdID09IDApIHsgY29udGludWU7IH0KICAgICAgVlBJSSBhbGxfbXlfY2VsbHM7CiAgICAgIFJFIChpaSwgaCkgewogICAgICAgIFJFIChqaiwgdykgewogICAgICAgICAgaWYgKGJvYXJkW2ldW2pdID09IGJvYXJkW2lpXVtqal0pIHsKICAgICAgICAgICAgYWxsX215X2NlbGxzLlBCKHtpaSwgamp9KTsKICAgICAgICAgIH0KICAgICAgICB9CiAgICAgIH0KICAgICAgc2V0PGNoYXI+IG5laXNfbGV0czsKICAgICAgZm9yIChhdXRvIHAgOiBhbGxfbXlfY2VsbHMpIHsKICAgICAgICBSRVAgKGRpciwgNCkgewogICAgICAgICAgaW50IG5pID0gcC5zdCArIGRpW2Rpcl07CiAgICAgICAgICBpbnQgbmogPSBwLm5kICsgZGpbZGlyXTsKICAgICAgICAgIGlmIChJc09uQm9hcmQobmksIG5qKSAmJiBhbnNbbmldW25qXSAhPSAnLicpIHsKICAgICAgICAgICAgbmVpc19sZXRzLmlu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ApIHsgYnJlYWs7IH0KICAgIGludCBpID0gbnh0X2NlbGwuc3Q7CiAgICBpbnQgaiA9IG54dF9jZWxsLm5kOwogICAgaWYgKGJvYXJkW2ldW2pdKSB7IGNvbnRpbnVlOyB9CiAgICBpbnQgemVyb19jbnQgPSAwLCBiYWRfcG9zID0gMDsKICAgIFJFUCAocGhhc2UsIDMpIHsKICAgICAgaW50IEwgPSB0aHJlc1twaGFzZV07CiAgICAgIGludCBSID0gdGhyZXNbcGhhc2UgKyAxXTsKICAgICAgUkVQICh0cnlfaW5kLCB0cmllc1twaGFzZV0pIHsKICAgICAgICBpbnQgdGlsZV9pbmQgPSBMICsgcmFuZCgpICUgKFIgLSBMKTsKICAgICAgICBpZiAoY250W3RpbGVfaW5kXS5uZCA9PSAwKSB7IHplcm9fY250Kys7IGNvbnRpbnVlOyB9CiAgICAgICAgdmVjdG9yPFZQSUk+IGFsbF9wb3NzID0gR2VuQWxsKGNudFt0aWxlX2luZF0uc3QpOwogICAgICAgIHJhbmRvbV9zaHVmZmxlKEFMTChhbGxfcG9zcykpOwogICAgICAgIGZvciAoYXV0byBwb3NzIDogYWxsX3Bvc3MpIHsKICAgICAgICAgIGZvciAoYXV0byBzaGlmdCA6IHBvc3MpIHsKICAgICAgICAgICAgYXV0byBucG9zcyA9IHBvc3M7CiAgICAgICAgICAgIGZvciAoYXV0byYgcCA6IG5wb3NzKSB7CiAgICAgICAgICAgICAgcC5zdCAtPSBzaGlmdC5zdDsKICAgICAgICAgICAgICBwLm5kIC09IHNoaWZ0Lm5kOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGludCBmYWlsID0gMDsKICAgICAgICAgICAgZm9yIChhdXRvIGNlbGwgOiBucG9zcykgewogICAgICAgICAgICAgIGludCBuaSA9IGkgKyBjZWxsLnN0OwogICAgICAgICAgICAgIGludCBuaiA9IGogKyBjZWxsLm5kOwogICAgICAgICAgICAgIGlmICghSXNPbkJvYXJkKG5pLCBuaikgfHwgYm9hcmRbbmldW25qXSkgeyBmYWlsID0gMTsgYnJlYWs7IH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoZmFpbCkgewogICAgICAgICAgICAgIGJhZF9wb3MrKzsKICAgICAgICAgICAgICBjb250aW51ZTsKICAgICAgICAgICAgfQogICAgICAgICAgICBmb3IgKGF1dG8gY2VsbCA6IG5wb3NzKSB7CiAgICAgICAgICAgICAgaW50IG5pID0gaSArIGNlbGwuc3Q7CiAgICAgICAgICAgICAgaW50IG5qID0gaiArIGNlbGwubmQ7CiAgICAgICAgICAgICAgYm9hcmRbbmldW25qXSA9IG54dF9pbmQ7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgc2V0PFBJST4gbmVpczsKICAgICAgICAgICAgZm9yIChhdXRvIGNlbGwgOiBucG9zcykgewogICAgICAgICAgICAgIGludCBuaSA9IGkgKyBjZWxsLnN0OwogICAgICAgICAgICAgIGludCBuaiA9IGogKyBjZWxsLm5kOwogICAgICAgICAgICAgIFJFUCAoZGlyLCA4KSB7CiAgICAgICAgICAgICAgICBpbnQgbm5pID0gbmkgKyBkaVtkaXJdOwogICAgICAgICAgICAgICAgaW50IG5uaiA9IG5qICsgZGpbZGlyXTsKICAgICAgICAgICAgICAgIGlmIChJc09uQm9hcmQobm5pLCBubmopICYmIGJvYXJkW25uaV1bbm5qXSA9PSAwKSB7CiAgICAgICAgICAgICAgICAgIG5laXMuaW5zZXJ0KHtubmksIG5uan0pOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQovLyAgICAgICAgICAgICBpZiAobmVpcy5lbXB0eSgpKSB7Ci8vICAgICAgICAgICAgICAgZGVidWcobnh0X2NlbGwpOwovLyAgICAgICAgICAgICAgIFByaW50Qm9hcmQoYm9hcmQpOwovLyAgICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChuZWlzLmVtcHR5KCkpIHsgYXNzZXJ0KHJlbV9hcmVhID09IFNaKG5wb3NzKSk7IGdvdG8gRHVwYTsgfQogICAgICAgICAgICBhc3NlcnQoIW5laXMuZW1wdHkoKSk7CiAgICAgICAgICAgIHZlY3RvcjxQSUk+IHF1ZXsqbmVpcy5iZWdpbigpfTsKICAgICAgICAgICAgbWFwPFBJSSwgaW50PiB2aXNfbmVpczsKICAgICAgICAgICAgdmlzX25laXNbe3F1ZVswXS5zdCwgcXVlWzBdLm5kfV0gPSAxOwogICAgICAgICAgICBmb3IgKGludCBpaSA9IDA7IGlpIDwgU1oocXVlKTsgaWkrKykgewogICAgICAgICAgICAgIGludCBjaSA9IHF1ZVtpaV0uc3Q7CiAgICAgICAgICAgICAgaW50IGNqID0gcXVlW2lpXS5uZDsKICAgICAgICAgICAgICBSRVAgKGRpciwgNCkgewogICAgICAgICAgICAgICAgaW50IG5pID0gY2kgKyBkaVtkaXJdOwogICAgICAgICAgICAgICAgaW50IG5qID0gY2ogKyBkaltkaXJdOwogICAgICAgICAgICAgICAgaWYgKG5laXMuY291bnQoe25pLCBuan0pKSB7CiAgICAgICAgICAgICAgICAgIGlmICh2aXNfbmVpc1t7bmksIG5qfV0gPT0gMCkgewogICAgICAgICAgICAgICAgICAgIHZpc19uZWlzW3tuaSwgbmp9XSA9IDE7CiAgICAgICAgICAgICAgICAgICAgcXVlLlBCKHtuaSwgbmp9KTsKICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoU1oocXVlKSA9PSBTWihuZWlzKSkgewogICAgICAgICAgICAgIGNudFt0aWxlX2luZF0ubmQtLTsKICAgICAgICAgICAgICByZW1fYXJlYSAtPSBTWihjbnRbdGlsZV9pbmRdLnN0KTsKICAgICAgICAgICAgICBnb3RvIEVuZDsKICAgICAgICAgICAgfQovLyAgICAgICAgICAgICBQcmludEJvYXJkKGJvYXJkKTsKLy8gICAgICAgICAgICAgZGVidWcobmVpcyk7Ci8vICAgICAgICAgICAgIGRlYnVnKHF1ZSk7CiAgICAgICAgICAgIGZv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5kICsgaCk7IAogICAgaWYgKG1MICE9IG1SKSB7CiAgICAgIHJldHVybiBtTCA8IG1SOwogICAgfQogICAgcmV0dXJuIEwgPCBSOwogIH0pOwogIC8vZGVidWcoY2VsbHNfb3JkZXIpOwogIAogIFJFIChpLCAyICogaCkgewogICAgUkUgKGosIDIgKiB3KSB7CiAgICAgIGNpbj4+YmlnX2JvYXJkW2ldW2pdOwogICAgfQogIH0KICBtYXA8VlBJSSwgaW50PiBjbnQ7CiAgaW50IGNudF90aWxlcyA9IDA7CiAgaW50IGNudF9jZWxscyA9IDA7CiAgVkkgY250X2J5X3N6KDcpOwogIFJFIChpLCAyICogaCkgewogICAgUkUgKGosIDIgKiB3KSB7CiAgICAgIGlmIChiaWdfYm9hcmRbaV1bal0gPT0gJy4nKSB7IGNvbnRpbnVlOyB9CiAgICAgIGlmICh2aXNbaV1bal0pIHsgY29udGludWU7IH0KICAgICAgVlBJSSBwb3M7CiAgICAgIERmc0JpZyhpLCBqLCBwb3MpOwogICAgICBwb3MgPSBVbmFtYmlndW91cyhwb3MpOwogICAgICBjbnRfY2VsbHMgKz0gU1oocG9zKTsKICAgICAgY250X2J5X3N6W1NaKHBvcyldKys7CiAgICAgIGNudF90aWxlcysrOwogICAgICBjbnRbcG9zXSsrOwogICAgfQogIH0KLy8gICBkZWJ1ZyhjbnRfdGlsZXMsIGggKiB3KTsKLy8gICBkZWJ1ZyhjbnQpOwovLyAgIGRlYnVnKFNaKGNudCkpOwovLyAgIGZvciAoYXV0byBwIDogY250KSB7Ci8vICAgICB2ZWN0b3I8dmVjdG9yPGNoYXI+PiB2aXN1KDUsIHZlY3RvcjxjaGFyPig1LCAnLicpKTsKLy8gICAgIGZvciAoYXV0byBwYXJhIDogcC5zdCkgewovLyAgICAgICB2aXN1W3BhcmEuc3RdW3BhcmEubmRdID0gJyMnOwovLyAgICAgfQovLyAgICAgUkVQIChpLCA1KSB7Ci8vICAgICAgIFJFUCAoaiwgNSkgewovLyAgICAgICAgIGNlcn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