#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define MAXN 100
char map[MAXN][MAXN];
char ans[MAXN][MAXN];
int N, M;
struct node {
string s;
int height;
int width;
bool operator < (const node& a) {
return s < a.s;
}
};
vector<node> v;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> map[i][j];
ans[i][j] = '.';
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (map[i][j] >= 'a' && map[i][j] <= 'z') {
int idx = j;
int height = 1;
int width = 1;
string tmp_s = "";
while (idx > 0 && map[i][idx--] != '+') width++;
idx = j;
while (map[i][idx] != '+') {
if (map[i][idx] >= 'a' && map[i][idx] <= 'z') tmp_s += map[i][idx];
width++;
idx++;
}
int tmp_j = j;
j = idx;
idx = i;
while (map[idx++][tmp_j] != '-') height++;
v.push_back({ tmp_s,height,width });
}
}
}
sort(v.begin(), v.end());
int sx = 0;
int sy = 0;
for (int i = 0; i < v.size(); i++) {
int height = v[i].height;
int width = v[i].width;
string tmp_s = v[i].s;
for (int x = sx; x < sx + height; x++) {
for (int y = sy; y < sy + width; y++) {
ans[x][y] = '.';
}
}
int start_title = sy+(width - tmp_s.length()) / 2;
for (int y = sy; y < start_title; y++) {
if (y == sy) ans[sx][y] = '+';
else if (y == start_title - 1) ans[sx][y] = '|';
else ans[sx][y] = '-';
}
for (int y = 0; y < tmp_s.length(); y++) {
ans[sx][y + start_title] = tmp_s[y];
}
for (int y = start_title + tmp_s.length(); y < sy + width; y++) {
if (y == start_title + tmp_s.length()) ans[sx][y] = '|';
else if (y == sy + width - 1) ans[sx][y] = '+';
else ans[sx][y] = '-';
}
for (int x = sx + 1; x < sx + height; x++) {
if (x >= sx + 1 && x < sx + height - 1) {
ans[x][sy] = '|';
ans[x][sy + width - 1] = '|';
}
else {
for (int y = sy; y < sy + width; y++) {
if (y == sy || y == sy + width - 1) ans[x][y] = '+';
else ans[x][y] = '-';
}
}
}
sx++;
sy++;
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++){
cout << ans[i][j];
}
cout << "\n";
}
return 0;
}