Result

AC

Duration

3150ms

Code [DL]

s=`dd`
puts s.gsub(/\n.{#{s=~/T/}}/,"\\0T").sub /K */{|y|"K"*y.size}

stdin

                   T                              
                                                  
                                                  
                                                  
                                                  
                                                  
              K                                   

stdout

                   T                              
                   T                               
                   T                               
                   T                               
                   T                               
                   T                               
              KKKKKT                               

stderr

0+1 records in
0+1 records out
357 bytes copied, 3.3628e-05 s, 10.6 MB/s

strace