/*
ID: niepeng1
PROG: starry
LANG: C++
*/
/*
记录把每个矩形块记录,然后按照四个方向匹配,其实也不复杂奥,就是,难道是我想的太少了。
其实细节方面处理也很麻烦的。
只是为什么我写的用char的一直会错误。
而用int的就会正确的呢?
*/
#include <stdio.h>
struct node{
int w;
int h;
int con[101][101];
}model[40];
int before[100][101],after[100][101],temp[100][101];
int lx,ly,rx,ry,w,h,modnum,now;
bool same(int t,int th,int tw)
{
int i,j;
bool can;
if(! ( (model[t].h==th&&model[t].w==tw) || (model[t].w==th&&model[t].h==tw)) )
return false;
if( model[t].h==th&&model[t].w==tw ){
can=true;
for(i=0;i<model[t].h;i++){
for(j=0;j<model[t].w;j++)
if(model[t].con[i][j]!=temp[i][j] && (model[t].con[i][j]=='0' || temp[i][j]=='0') )
{
can=false;
break;
}
if(!can)
break;
}
if(can) return true;
can=true;
for(i=0;i<model[t].h;i++){
for(j=0;j<model[t].w;j++)
if( model[t].con[i][j]!=temp[model[t].h-1-i][model[t].w-1-j] && (model[t].con[i][j]=='0' || temp[model[t].h-1-i][model[t].w-1-j]=='0')){
can=false;
break;
}
if(!can)
break;
}
if(can) return true;
can=true;
for(i=0;i<model[t].h;i++){
for(j=0;j<model[t].w;j++)
if( model[t].con[i][j]!=temp[model[t].h-1-i][j] && (model[t].con[i][j]=='0' || temp[model[t].h-1-i][j]=='0')){
can=false;
break;
}
if(!can)
break;
}
if(can) return true;
can=true;
for(i=0;i<model[t].h;i++){
for(j=0;j<model[t].w;j++){
if( model[t].con[i][j]!=temp[i][model[t].w-1-j] && (model[t].con[i][j]=='0' || temp[i][model[t].w-1-j]=='0')){
can=false;
break;
}
}
if(!can)
break;
}
if(can) return true;
}
if( model[t].w==th&&model[t].h==tw ){
can=true;
for (i=0;i<model[t].h;i++)
{
for (j=0;j<model[t].w;j++)
if (model[t].con[i][j]!=temp[j][i]&&(model[t].con[i][j]=='0'||temp[j][i]=='0'))
{
can=false;
break;
}
if (!can) break;
}
if (can) return true;
can=true;
for(i=0;i<model[t].h;i++){
for(j=0;j<model[t].w;j++)
if(model[t].con[i][j]!=temp[model[t].w-j-1][i] && (model[t].con[i][j]=='0'||temp[model[t].w-j-1][i]=='0'))
{
can=false;
break;
}
if(!can) break;
}
if(can) return true;
can=true;
for (i=0;i<model[t].h;i++)
{
for (j=0;j<model[t].w;j++)
if (model[t].con[i][j]!=temp[j][model[t].h-i-1]&&(model[t].con[i][j]=='0'||temp[j][model[t].h-i-1]=='0'))
{
can=false;
break;
}
if (!can) break;
}
if (can) return true;
can=true;
for (i=0;i<model[t].h;i++)
{
for (j=0;j<model[t].w;j++)
if (model[t].con[i][j]!=temp[model[t].w-j-1][model[t].h-i-1]&&(model[t].con[i][j]=='0'||temp[model[t].w-j-1][model[t].h-i-1]=='0'))
{
can=false;
break;
}
if (!can) break;
}
if (can) return true;
}
return false;
}
int find(int th,int tw)
{
int i,j,t;
for(t=0;t<=modnum;t++)
if(same(t,th,tw)) return t+'a';
modnum++;
model[modnum].w=tw;
model[modnum].h=th;
for(i=0;i<th;i++)
for(j=0;j<tw;j++)
model[t].con[i][j]=temp[i][j];
return t+'a';
}
void flood_fill(int x,int y,int num)
{
before[x][y]=num+'0';
if(x-1>=0&&y-1>0&&before[x-1][y-1]=='1') {flood_fill(x-1,y-1,num); if(x-1<lx) lx=x-1;if(y-1<ly) ly=y-1;}
if(x-1>=0&&before[x-1][y]=='1') {flood_fill(x-1,y,num); if(x-1<lx) lx=x-1;}
if(x-1>=0&&y+1<w&&before[x-1][y+1]=='1') {flood_fill(x-1,y+1,num); if(x-1<lx) lx=x-1;if(y+1>ry) ry=y+1;}
if(x+1<h && before[x+1][y]=='1') {flood_fill(x+1,y,num); if(x+1>rx) rx=x+1;}
if(x+1<h&&y-1>=0&&before[x+1][y-1]=='1') {flood_fill(x+1,y-1,num); if(x+1>rx) rx=x+1;if(y-1<ly) ly=y-1;}
if(x+1<h&&y+1<w&&before[x+1][y+1]=='1') {flood_fill(x+1,y+1,num); if(x+1>rx) rx=x+1;if(y+1>ry) ry=y+1;}
if(y-1>=0&&before[x][y-1]=='1') {flood_fill(x,y-1,num); if(y-1<ly) ly=y-1;}
if(y+1<w &&before[x][y+1]=='1') {flood_fill(x,y+1,num); if(y+1>ry) ry=y+1;}
}
void oper(int x,int y,int num)
{
int i,j;
int ch;
lx=rx=x;ly=ry=y;
flood_fill(x,y,num);
for(i=lx;i<=rx;i++)
for(j=ly;j<=ry;j++)
if(before[i][j]!='0'+num) temp[i-lx][j-ly]='0';//这个非常的精辟,我又忽略了
else temp[i-lx][j-ly]='0'+num;
ch=find(rx-lx+1,ry-ly+1);
for(i=lx;i<=rx;i++)
for(j=ly;j<=ry;j++)
if(before[i][j]=='0'+num) after[i][j]=ch;
}
int main()
{
freopen("starry.in","r",stdin);
freopen("starry.out","w",stdout);
char c;
int i,j;
scanf("%d %d",&w,&h);
c=getchar();
for(i=0;i<h;i++)
{
for (j=0;j<w;j++)
scanf("%c",&before[i][j]);
c=getchar();
}
modnum=-1;now=2;
for(i=0;i<h;i++)
for(j=0;j<w;j++)
{
if(before[i][j]=='0') after[i][j]='0';
else if(before[i][j]=='1') oper(i,j,now++);
}
for(i=0;i<h;i++)
{
for(j=0;j<w;j++)
printf("%c",after[i][j]);
printf("/n");
}
return 0;
}