s=[];gets.gsub(/\W|.../){next s+=[h=$&[1].to_i,w=$&[0].to_i,[$&[2]*w]*h]if$&[1];*s,h,w,a,r,c,b=s;s+=$&<'|'?[h<r ?h=r :h,w+c,(a+['.'*w]*h).zip(b+['.'*c]*h)[0,h].map(&:join)]:[h+r,w<c ?w=c :w,(a+b).map{|x|(x+'.'*w)[0,w]}]};puts s[2]
Note that non-ascii characters in the above source code will be escaped (such as \x9f).