<?xml version="1.0" encoding="Windows-1251"?>
<CATS version="1.2">
<Problem title="Terrarium" lang="en" author="A. Klenin, T. Chistyakov" tlimit="3" mlimit="8"
  inputFile="input.txt" outputFile="output.txt" stdChecker="longstrs">

<ProblemStatement>
 <p>A zoology research lab has a terrarium with rare species of snakes.
 Terrarium is a flat box filled with soil, and has a glass top allowing to watch the snakes.
 There are trenches in the soil, and snakes constantly move along the trenches.
 All snakes have diameter of 1 cm and integer length of no less than 2 cm.
 </p><p>
 While watching the snakes, the zoologists discovered a pattern in their movement: 
each snake moves at a speed of 1 cm per second forward, until it encounters either
a wall or another snake. Faced an obstacle, snake first tries to turn right, if there
is also obstacle on the right, then it tries to turn left. If there is obstacle on the left also,
the snake waits for a second before trying to move again.
</p><p>
In order to validate the discovery, it was decided to write a program that simulates snakes'
behaviour. This task was assigned to you.
</p><p>The terrarium is represented by an array of $N \times N$ characters.
Each character is one of:
<ul>
  <li>'.' (ASCII 46) -- trench</li>
  <li>'#' (ASCII 35) -- wall</li>
  <li>'A' to 'Z' -- snake's head</li>
  <li>'a' to 'z' -- snake's body or tip of the tail</li>
</ul>
Snakes are represented by latin letters, so there are no more than 26 snakes in terrarium.
Snakes try to move in alphabetical order every second.
</p><p>
Your program must output the state of the terrarium after $T$ seconds.
</p><p>
</p>
</ProblemStatement>

<ProblemConstraints>
$2 \le N \le 1000$, $1 \le T \le 10^6$.
</ProblemConstraints>

<InputFormat>
First line of input file contains integers $N T$.
Following $N$ lines contain $N$ characters each -- the initial state of the terrarium.
The input file guarantees unambiguous recognition of snakes -- all snakes are continuous,
every character of snake's body has exactly two neighbour characters belonging
to the same snake, while head and tip of the tail have exactly one.
</InputFormat>

<OutputFormat>
Output file must contain $N$ lines of $N$ characters --
the state of the terrarium after $T$ seconds.
</OutputFormat>

<!-- <Sample rank="1">
  <SampleIn>3 3
...
...
aaA</SampleIn>
  <SampleOut>.Aa
..a
...</SampleOut>
</Sample> -->
<Sample rank="1">
  <SampleIn>4 8
.bB.
....
a.#.
aaA.</SampleIn>
  <SampleOut>.baa
.BAa
..#.
....</SampleOut>
</Sample>
<Sample rank="2">
  <SampleIn>7 100000
aA.....
a#####D
aa....d
......d
......d
......d
.......</SampleIn>
  <SampleOut>aaaaADd
.#####d
......d
......d
.......
.......
.......</SampleOut>
</Sample>

<Solution name="sol" src="terrarium_ask.dpr"/>
<Solution name="sol_tim" src="terrarium_tim.dpr"/>
<Generator name="g1" src="terr_gen.dpr" outputFile="*STDOUT"/>
<Generator name="g2" src="terr_gen2.dpr" outputFile="*STDOUT"/>
<TestRange from="1" to="17"><In src="%0n"/><Out use="sol"/></TestRange>

<Test rank="18"><In use="g1" param="100 10"/><Out use="sol"/></Test>
<Test rank="19"><In use="g1" param="500 100"/><Out use="sol"/></Test>
<Test rank="20"><In use="g1" param="1000 1000"/><Out use="sol"/></Test>
<Test rank="21"><In use="g1" param="1000 1000000"/><Out use="sol"/></Test>

<Test rank="22"><In use="g2" param="100 10"/><Out use="sol"/></Test>
<Test rank="23"><In use="g2" param="500 100"/><Out use="sol"/></Test>
<Test rank="24"><In use="g2" param="1000 1000"/><Out use="sol"/></Test>
<Test rank="25"><In use="g2" param="1000 1000000"/><Out use="sol"/></Test>

</Problem>
</CATS>
