1624 - Beautiful Sky

Time Limit: 1s Memory Limit: 128MB

Submissions: 57 Solved: 19
Description
The 8th(2013) ACM Programming Contest of HUST Problem Set
Little Mine is a naughty boy. He was once an excellent student and has won a great many prizes since the primary school. However, he’s also addicted to video games. After he goes to college, when it comes to physics, his performance turns out the opposite way. 
His teacher Mr. Wang is a nice person. When hearing about Littlemine’s story, he decided to offer him an “opportunity”. If he could solve the difficult problem, he wouldn’t have to hand in his homework for the whole semester(ACTUALLY Mr.Wang WANTS HIM TO FIND A WAY BACK TO BE A GOOD STUDENT). Here is the problem:
This is a map consisting of multiple constellations(A constellation means a union of connected stars, each one of them has at least one star in its eight adjacent positions, and one constellation cannot be a part of another bigger constellation). Two constellations are similar if and only if they have same amount of stars and exact the same shape, ignoring the difference of their directions.

Here we use a n*m (0<=n,m<=100) matrix consisting of ones and zeros to denote the map. One means there’s a star in its position (the total number of stars won’t exceed 500), while zero means nothing. Given such a matrix, you need to use a lower-case letter to denote all the constellations. Similar constellations must be marked with the same letter. There are at most 26 different constellations and each one has at most 160 stars. Check the example below to learn more details. 
But Little Mine doesn’t buy it, he makes up his mind to put things “right” once and for all. So he asks you for help. Will you help him?
Input
The first line is an integer T ( T <= 5) meaning there are T test cases. For each test case, the first two lines have two numbers, m and n, which represents the width and the height of the map. The next n lines describe the map, each line has exact m characters. 
Output
For each test case, output the related marked map. If there are multiple solutions, output the one with minimum lexicographic order. There is at least one blank line  between each two cases.
Sample Input
1
23
15
10001000000000010000000
01111100011111000101101
01000000010001000111111
00000000010101000101111
00000111010001000000000
00001001011111000000000
10000001000000000000000
00101000000111110010000
00001000000100010011111
00000001110101010100010
00000100110100010000000
00010001110111110000000
00100001110000000100000
00001000100001000100101
00000001110001000111000
Sample Output
a000a0000000000b0000000
0aaaaa000ccccc000d0dd0d
0a0000000c000c000dddddd
000000000c0b0c000d0dddd
00000eee0c000c000000000
0000e00e0ccccc000000000
b000000e000000000000000
00b0f000000ccccc00a0000
0000f000000c000c00aaaaa
0000000ddd0c0b0c0a000a0
00000b00dd0c000c0000000
000g000ddd0ccccc0000000
00g0000ddd0000000e00000
0000b000d0000f000e00e0b
0000000ddd000f000eee000
Hint
Source
The 8th(2013) ACM Programming Contest of HUST