R=raw_input while 1: w=len(R())-2;h=0 while" "in R():h+=1 r,c=map(int,R().split());X=lambda a,b:((" "+a+w/c*b+a)*c)[1:]+"\n";e=X(*"+-");print(e+h/r*X(*"| ")+e)*r,

Note that non-ascii characters in the above source code will be escaped (such as \x9f).