Четвёртый открытый Зеленоградский турнир 2008

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
 

#define MIN(a,b) ((a)<(b)?(a):(b))

#define THRESHOLD	0.15

#define N_AR	26
#define N_COUR	25
#define N_TNR	25

#define INF			10000000

#define TOP		0
#define BOTTOM		1
#define LEFT		2
#define RIGHT		3
#define TOPLEFT		4
#define TOPRIGHT	5
#define BOTLEFT		6
#define BOTRIGHT	7

#define INPUT_H		200
#define INPUT_W		200

#define HEIGHT		20

char s[INPUT_H][INPUT_W];

char rgn[2*INPUT_H][2*INPUT_H];

double tilt, angle;

double Mx, My, min_Mx, min_My, max_Mx, max_My;

double Sq, min_Sq;

FILE *f;

struct Point {

	int x;
	int y;

};

struct Rectangle {

	struct Point c1;
	struct Point c2;

};

struct RectData {

	double angle;

	double min_Mx;

	double max_Mx;

	double min_My;

	double max_My;

	int w;

	int h;

	int sgn;

};

 

struct RectData rdata;

struct Point corners[8];

char vals_arial[N_AR] = "ZYXWVUTSRQPONMLKJIHGFEDCBA";
char vals_courier[N_COUR] = "ZYXWVUTSRPONMLKJIHGFEDCBA";
char vals_tnr[N_TNR] = "ZYXWVUTSRPONMLKJIHGFEDCBA";

char **let;
char *let_val;
int N;

char *arial[N_AR] =

{

"000..............000"
"X00..............0XX"
"XXX0.............0XX"
"XXXX0............0XX"
"XX0XX0...........0XX"
"XX00XX00.........0XX"
"XX0000XX0........0XX"
"XX0...0XX00......0XX"
"XX0....00X00.....0XX"
"XX0.....00XX0....0XX"
"XX0......00XX0...0XX"
"XX0........0XX00.0XX"
"XX0.........0XXX00XX"
"XX0..........0XXX0XX"
"XX0............0XXXX"
"000.............0XXX"
"....................",

"...................."
"00.................."
"0X00................"
"0XXX0..............."
"00XXX00............."
"..0XXXX00..........."
"...00XXXX0.........."
".....00XXX0000000000"
".......00XXXXXXXXXX0"
".......00XXXXXXXXXX0"
".....00XXX0000000000"
"....0XXXX0.........."
"..00XX000..........."
".0XXXX0............."
"0XXX00.............."
"0X00................"
"00.................."
"....................",

"...................."
"00...............000"
"XX0............00XXX"
"XXX00.........0XXXX0"
"0XXXX0......00XXX00."
".00XXX00...0XXXX0..."
"...0XXXX000XXX00...."
"....00XXXXXX00......"
"......00XXX00......."
"....00XXXXXXX0......"
"...0XXXX000XXX00...."
"..0XXX00...0XXXX00.."
".0XX00......00XXXX0."
"0XX00.........0XXXX0"
"XX0............00XXX"
"X0..............00XX"
"0.................00",

"...................."
"0000................"
"XXXX00000..........."
"000XXXXXX0000......."
"...0000XXXXXX0000..."
".......0000XXXXXX000"
"...........0000XXXXX"
"..............00XXXX"
"...........000XXXXXX"
"........000XXXXX0000"
".....000XXXXX000...."
"...00XXXXXX00......."
"000XXXX0000........."
"XXXXX00............."
"XXX00..............."
"XXXXX000............"
"000XXXXX000........."
"...000XXXXX000......"
"......000XXXXX000..."
".........00XXXXXX000"
"...........000XXXXXX"
"..............000XXX"
"...........0000XXXXX"
"......00000XXXXX0000"
"..0000XXXXXXX000...."
"00XXXXXXX0000......."
"XXXX00000..........."
"0000................",

"00000..............."
"0XXXX00............."
"00XXXXX000.........."
"..000XXXXX00........"
".....00XXXXX000....."
".......000XXXXX00..."
"..........00XXXXX000"
"............000XXXXX"
"...............00XXX"
"............0000XXXX"
"..........00XXXXXX00"
".......000XXXX0000.."
"....000XXXXXX0......"
"..00XXXXXX000......."
"000XXXX000.........."
"0XXXX00............."
"00000..............."
"....................",

"...................."
"0000000000000000...."
"0XXXXXXXXXXXXXXXX0.."
"0000000000000000XX0."
"...............00XX0"
"...............00XXX"
"................00XX"
"................00XX"
"................00XX"
"................00XX"
"................00XX"
"................00XX"
"...............00XX0"
"000000000000000XXXX0"
"0XXXXXXXXXXXXXXXX00."
"0000000000000000...."
"....................",

"000................."
"XX0................."
"XX0................."
"XX0................."
"XX0................."
"XX0................."
"XX000000000000000000"
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXX"
"XX000000000000000000"
"XX0................."
"XX0................."
"XX0................."
"XX0................."
"XX0................."
"000.................",

"...................."
"..00000....0000000.."
".00XXX0...0XXXXXXX0."
".0XX00...0XX0000XX0."
"0XX0....00XX0...0XX0"
"0X0.....00X0....0XX0"
"XX0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"XX0....00XX0.....0XX"
"XX0....00X0......0XX"
"XX0....0XX0......0XX"
"0X0....0XX0......0XX"
"0XX0..0XXX0.....0XX0"
"0XX0000XX0.....0XX00"
".0XXXXXX00...00XX00."
"..00XXX00...00XX00.."
"....000.....0XX00..."
"............0000....",

"....0000............"
"..00XXXX0........000"
".0XXXXXX00.....00XXX"
"0XXX000XXX0...0XXXX0"
"XXX0...0XX0..0XXX00."
"0X0.....0X000XX00..."
"XX0.....0XXXXXX0...."
"XX0.....0XXXX00....."
"XX0.....0XXX0......."
"XX0.....0XX0........"
"XX0.....0XX0........"
"XX0.....0XX0........"
"XX0.....0XX0........"
"XX0.....0XX0........"
"XX0000000XX000000000"
"XXXXXXXXXXXXXXXXXXXX"
"00000000000000000000"
"....................",

"...................."
".....00000000000..0X"
"...00XXXXXXXXXXX00XX"
"..0XXXX0000000XXXXXX"
".0XX000.......00XXX0"
".0XX0..........0XXX0"
"0XX0...........0XXX0"
"0XX0..........0XXXXX"
"XX0...........0XX0X0"
"XX0...........0X00XX"
"XX0............0.0XX"
"XX0..............0XX"
"XX0..............0XX"
"0X0..............0X0"
"0XX0............0XX0"
"0XX00...........0X0."
".0XXX0........00XX0."
"..0XXX00000000XXX0.."
"...00XXXXXXXXXXX0..."
".....00000000000...."
"....................",

"...................."
"..0XXXXX00.........."
".0XXXXXXXX0........."
"0XX000000XX0........"
"XXX0....0XX0........"
"0X0......0XX0......."
"XX0......0XX0......."
"XX0......0XX0......."
"XX0......0XX0......."
"XX0......0XX0......."
"XX0......0XX0......."
"XX0......0XX0......."
"XX00000000XX00000000"
"XXXXXXXXXXXXXXXXXXXX"
"00000000000000000000"
"....................",

"........00000......."
".....000XXXXX000...."
"....0XXXXXXXXXX000.."
"..0XXX00000000XXX00."
".0XX000......00XXX0."
"0XX00.........000X0."
"0X00...........00XX0"
"0X00...........00XX0"
"XX00............00XX"
"XX00............00XX"
"XX00............00XX"
"XX00............00XX"
"XX00............00XX"
"0X00............00XX"
"0XX00..........00XX0"
"0XX000........00XX0."
"00XXX00......00XXX0."
"..0XXX00000000XXX00."
"...00XXXXXXXXXXX00.."
".....00XXXXXX0000..."
"......00000000......",

"...................."
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXX"
"0000000000000000XXX0"
".............00XX00."
"............0XXX0..."
"..........00XX00...."
".........0XXX0......"
".......00XX00......."
".....00XXX0........."
"....0XXX00.........."
"..00XX00............"
".0XXX0.............."
"0XX00000000000000000"
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXX"
"....................",

"...................."
"00000000000000000000"
"0XXXXXXXXXXXXXXXXXX0"
"0XX00000000000000000"
"00XXXX000..........."
"..00XXXXX0000......."
"....0000XXXXX0000..."
"........000XXXXXX000"
"...........000XXXXX0"
".............000XXX0"
"..........000XXXXXX0"
"........00XXXXXX000."
".....000XXXX0000...."
".0000XXXXX00........"
"0XXXXXX000.........."
"0XX00000000000000000"
"0XXXXXXXXXXXXXXXXXX0"
"00000000000000000000"
"....................",

"...................."
".................0X0"
".................0X0"
".................0X0"
".................0X0"
".................0X0"
".................0X0"
".................0X0"
".................0X0"
".................0X0"
"000000000000000000X0"
"0XXXXXXXXXXXXXXXXXX0"
"0XXXXXXXXXXXXXXXXXX0"
"....................",

"...................."
"X0...............0XX"
"XX0.............0XXX"
"0XX0..........00XXX0"
"0XXX0........0XXXX0."
".0XXX0.....00XXXX0.."
"..0XXX0...0XXXX00..."
"...00X00.0XXXX0....."
"....0XXX0XXX00......"
".....0XXXXX0........"
"......0XXX0........."
".......0XXX0........"
"........0XX0........"
"0000000000XXX0000000"
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXX"
"....................",

"...................."
"XXXXXXXXXXXXXXXX000."
"XXXXXXXXXXXXXXXXXXX0"
"00000000000000000XX0"
".................0XX"
".................0XX"
".................0XX"
"................00XX"
"..............000XX0"
".............0XXXXX0"
"..............0XX0.."
"...............000..",

"...................."
"00000000000000000000"
"0XXXXXXXXXXXXXXXXXX0"
"0XXXXXXXXXXXXXXXXXX0"
"00000000000000000000"
"....................",

"...................."
"00000000000000000000"
"XXXXXXXXXXXXXXXXXXXX"
"000000000XX000000000"
"........0XX0........"
"........0XX0........"
"........0XX0........"
"........0XX0........"
"........0XX0........"
"........0XX0........"
"........0XX0........"
"........0XX0........"
"........0XX0........"
"000000000XX000000000"
"XXXXXXXXXXXXXXXXXXXX"
"00000000000000000000"
"....................",

"...................."
"...000...0XXXXXXX..."
"..0XXX0..0XXXXXXXX.."
".0XXX0...0XX0000XX.."
"0XX00....0XX0...0XX."
"0XX0.....0XX0....0X."
"0X0......0XX0....0XX"
"XX0......0000....0XX"
"XX0......0000....0XX"
"XX0..............0XX"
"XX0..............0XX"
"0X0..............0XX"
"XX0.............0XX0"
"0XX0............0XX0"
".0XX0..........0XXX0"
".0XX00.......00XX00."
"..0XXX0000000XXXX0.."
"...00XXXXXXXXXX00..."
"....00X0XXXXX00.....",

"...................."
"000................."
"XX0.....0000........"
"XX0.....0XX0........"
"XX0.....0XX0........"
"XX0.....0XX0........"
"XX0.....0XX0........"
"XX0.....0XX0........"
"XX0.....0XX0........"
"XX0.....0XX0........"
"XX0.....0XX0........"
"XX0.....0XX0........"
"XX0000000XX000000000"
"XXXXXXXXXXXXXXXXXXXX"
"00000000000000000000"
"....................",

"...................."
"XX0..............0XX"
"XX0.....0000.....0XX"
"XX0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"XX0000000XX0000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"00000000000000000000"
"....................",

"...................."
"....00000000000....."
"..00XXXXXXXXXXX00..."
".0XXXX0000000XXXX0.."
".0XX00.......000XX0."
"0XX0............0XX0"
"0X0.............0XX0"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0000000000000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXX"
"....................",

"...................."
"...00X0......0X00..."
"..0XXX0.....0XXXX0.."
".0XXX0.......00XX0.."
"0XX00..........0XX0."
"0XX0............0XX0"
"XX0.............0XXX"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"0XX0............0XXX"
".0X00...........0XX0"
".0XXX0.......000XX0."
"..0XX00000000XXXX0.."
"...0XXXXXXXXXXX00..."
"....00XXXXXXX00....."
".......000000.......",

"...................."
"...0000....00XXX00.."
"..0XXXX00.0XXXXXXX0."
".0XXXXXXX0XX0000XXX0"
"0XXX0000XXXX0...00X0"
"0X00....0XX0.....0X0"
"0X0.....0XX0.....0X0"
"0X0.....0XX0.....0X0"
"0X0.....0XX0.....0X0"
"0X0.....0XX0.....0X0"
"0X0.....0XX0.....0X0"
"0X0.....0XX0.....0X0"
"0X0.....0XX0.....0X0"
"0X0000000XX0000000X0"
"0XXXXXXXXXXXXXXXXXX0"
"00000000000000000000"
"....................",

"..................00"
"................0000"
".............000XXX0"
"..........000XXXXX00"
"........00XXXXX000.."
".....000XXX0XX0....."
"..000XXXX000XX0....."
"00XXXX0000.0XX0....."
"0XXX000....0XX0....."
"0X00.......0XX0....."
"0XX0000....0XX0....."
"00XXXX0000.0XX0....."
"..000XXXX000XX0....."
".....000XXX0XX0....."
"........00XXXXX000.."
"..........000XXXXX00"
".............00XXXX0"
"...............00000"
".................000"


};

char *courier[N_COUR] = 
{

"000.........00000000"
"0000........0XXXXXXX"
"XXX00.......00XXXXXX"
"XXXX00.......00000XX"
"XX00XX0..........0XX"
"XX0.0XX000.......0XX"
"XX0..0XXXX0......0XX"
"XX0...000XX00....0XX"
"XX0......0XXX0...0XX"
"XX0.......0XXX0..0XX"
"XX0........00XX000XX"
"XX000000.....0XXX0XX"
"XXXXXXX0......00XXXX"
"00000000........0XXX"
".................000",

"000................."
"XX0................."
"XX0................."
"XXX00............000"
"XXXXX00..........0XX"
"XX00XXX00........0XX"
"00..0XXXX0.......0XX"
".....00XXX00000000XX"
".......000XXXXXXXXXX"
".......00XXXXXXXXXXX"
".....00XXX00000000XX"
"00..0XXXX0.......0XX"
"XX00XX000........0XX"
"XXXXX0...........0XX"
"XXX00............000"
"XX0................."
"0X0................."
".0..................",

"00................00"
"XX0..............0XX"
"XX0.............00XX"
"XXX00..........0XXXX"
"XX0XX0........0XX0XX"
"XX00XX0.....00XX00XX"
"00..0XX000.0XXX0..00"
".....0XXXX0XX00....."
"......000XXX0......."
"......000XXX00......"
".....0XXXX0XXX0....0"
"00..0XX000.00XX0..0X"
"XX00XX0......0XX00XX"
"XXXXX0........0XXXXX"
"XXX00..........00XXX"
"XX0..............0XX"
"XX0..............0XX"
"00................00",

"...................."
"00.................."
"XX00000000000......."
"XXXXXXXXXXXXX0000000"
"XX00000XXXXXXXXXXXXX"
"XX0...0XXXXXXXXXXXXX"
"XX0....0000000000XXX"
"XX0........0000XXXXX"
"00.....0000XXXXX0000"
".....00XXXXXX000...."
"....0XXXX0000......."
"...0XXX00000........"
"....00XXXXXX000....."
"00....0000XXXXX0000."
"XX0.......0000XXXXX0"
"XX0........000000XXX"
"XX000000000XXXXXXXXX"
"XXXXXXXXXXXXXXX00000"
"XXXXX0000000000....."
"XX000...............",

"00.................."
"XX0................."
"XX0................."
"XXX000.............."
"XXXXXX0000.........."
"XX000XXXXX00........"
"XX0..00XXXXX000....."
"0X0....0000XXXX00..."
".0.........000XXX000"
"..............00XXXX"
"...............000XX"
".............00XXXX0"
"000.......000XXXX00."
"0X0....000XXXX000..."
"XX0.000XXXX000......"
"XX00XXXXX00........."
"XXXXX0000..........."
"XXX00..............."
"XX0................."
"00..................",

"000................."
"0X0................."
"XX00000000000000...."
"XXXXXXXXXXXXXXXX00.."
"XXXXXXXXXXXXXXXXXX0."
"XX000000000000000XX0"
"XX0..............0XX"
"XX0..............0XX"
"000..............0XX"
"..................0X"
"..................0X"
"..................0X"
"000...............0X"
"XX0..............0XX"
"XX0...........000XX0"
"XX000000000000XXXX0."
"XXXXXXXXXXXXXXXX00.."
"XX00000000000000...."
"XX0................."
"000.................",

"00000000............"
"0XXXXXX0............"
"XXXXXXX0............"
"XX000000.........000"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0000000000000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXX"
"XX0000000000000000XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX000000.........000"
"XXXXXXX0............"
"00000000............",

"...................."
"000000......0XXXX0.."
"XXXXXX0....0XXXXXX0."
"0XXX00....0XX0000XX0"
"0XX0.....0XX0....0XX"
"XX0......0XX0.....0X"
"XX0......0X0......0X"
"X0.......0X0......0X"
"X0......0XX0......0X"
"X0......0XX0......0X"
"XX0.....0XX0......0X"
"XX0....00X0......0XX"
"0XX0000XXX0......0XX"
".0XXXXXXX0.....00XX0"
"..0XXXX00.....0XXXXX"
"...0000.......0XXXX0"
"...............0000.",

"..................00"
".................0XX"
".................0XX"
"...000000......00XXX"
"..0XXXXXX0....0XXX00"
".0XX000XXX0..0XX00.."
"0XX0...00XX00XX0...."
"0X0.....0XX0XX0....."
"XX0......0XXX0......"
"XX0......0XX0......."
"XX0......0XX0......0"
"XX0......0XX0.....0X"
"XX0......0XX0....0XX"
"XX00000000XX000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXX"
"XX0000000000000000XX"
"XX0..............0XX"
"000..............000",


"...................."
"..0XXXXXX00........."
".0XX000XXXX0........"
"0XX0...000X0........"
"0X0......0XX0......."
"0X0.......0X0......."
"XX0.......0X0.....0X"
"XX0.......0X0....0XX"
"XX0.......0X0....0XX"
"XX0.......0X0....0XX"
"XX000000000X000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXX"
"XX0000000000000000XX"
"XX0..............0XX"
"....................",

".....0000000000....."
"...00XXXXXXXXXX00..."
"..0XXXXXXXXXXXXXX0.."
".0XX000000000000XX0."
"0XX0............0XX0"
"0X0..............0XX"
"XX0..............00X"
"XX0..............00X"
"X00..............00X"
"XX0..............00X"
"XX0..............00X"
"0X0..............0XX"
"0XX0............0XX0"
".0XX00.........0XX0."
"..0XXX000000000XX0.."
"...0XXXXXXXXXXXX0..."
"....00XXXXXXXX00...."
"......00000000......"
"....................",


"00.................."
"XX000000000000000000"
"XXXXXXXXXXXXXXXXXXX0"
"XXXXXXXXXXXXXXXXXXXX"
"XX000000000000000XX0"
"XX0..........00XXX0."
"00.........00XXX00.."
"..........0XXX00...."
".......000XX00......"
".....00XXXX0........"
"....0XXXX00.......00"
"..00XX000........0XX"
".0XXX0...........0XX"
"0XX000000000000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XX0000000000000000XX"
"XX0..............0XX"
"00................00",

"..................00"
"00...............0XX"
"XX0000000000000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XX0000000000000000XX"
"0XXXX0...........0XX"
".00XXXX000.......0XX"
"...000XXXX000.....00"
"......000XXXX0......"
".........00XXX0....."
".........000XX0....."
"......000XXXX0......"
"....00XXXX000.....00"
".000XXX000.......0XX"
"0XXXX00..........0XX"
"XXX000000000000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXX"
"XX0000000000000000XX"
"00................00",

".............0000000"
"............0XXXXXX0"
"...........0XXXXXXXX"
"............000000XX"
".................0XX"
".................0XX"
".................0XX"
"00...............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0000000000000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XX0000000000000000XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"00................00",

"..................00"
"00...............0XX"
"XX0..............0XX"
"XXX0...........00XXX"
"XXX0.........00XXXX0"
"XX0X0.......0XXX000."
"XX0XX0.....0XX00...."
"00.0XX0...0XX0......"
"....0XX000XX0......."
".....0XXXXX0........"
"00....0XXX0.......00"
"XX0....00XX0.....0XX"
"XX0......0XX0....0XX"
"XX00000000XXX00000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XX0000000000000000XX"
"XX0..............0XX"
"00................00",

"00.................."
"XX0................."
"XX0................."
"XX0................."
"XX0000000000000....."
"XXXXXXXXXXXXXXX000.."
"XXXXXXXXXXXXXXXXXX0."
"XX000000000000000XX0"
"XX0..............0XX"
"XX0..............0XX"
"XX0...............0X"
"XX0...............0X"
"XX0...............0X"
"00...............0XX"
".................0XX"
"............00000XX0"
"...........0XXXXXX0."
"...........0000000.."
"............00000...",

"000..............000"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0000000000000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXX"
"XX0000000000000000XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"000..............000",

"...................0"
"00................0X"
"XX0000000000000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXX"
"XX0000000XX0000000XX"
"XX0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"00......0XX0......00"
"........0XX0........"
"........0XX0........"
"00......0XX0......00"
"XX0.....0XX0.....0XX"
"XX0000000XX0000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXX"
"XX0000000000000000XX"
"00...............0XX"
"..................00",

"...........000......"
".0000......0X000000."
"0XXXX0.....XXXXXXXX0"
"0XXXX0.....XXXXXXXX0"
"0XX00......XX00000XX"
"0X0........XX0...0XX"
"XX0........XX0....0X"
"XX0........XX0....0X"
"X0.........X00....0X"
"X0................0X"
"XX0...............0X"
"XX0..............0XX"
"0XX0.............0X0"
"0XX00...........0XX0"
".0XXX00......000XX0."
"..0XXXX000000XXXX0.."
"...000XXXXXXXXX00..."
"......000000000.....",

".0000000............"
"0XXXXX00............"
"XXXXX000............"
"XX000..............."
"XX0................."
"XX0................."
"XX0..000000000....00"
"XX0..0XXXXXXX0...00X"
"XX0..0000XX000...0XX"
"XX0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"XX0000000XX0000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXX"
"XX0000000000000000XX"
"XX0..............0XX"
"000...............00",

"...................."
"000000.......0000000"
"XXXXX0.......0XXXXXX"
"XX000.........0000XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0...0000000....0XX"
"XX0..0XXXXXXX0...0XX"
"XX0...000XX00....0XX"
"XX0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"XX0000000XX0000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XX0000000000000000XX"
"XX0..............0XX"
"00...............000",


"...................."
"...00XXXXXXXXXX00..."
"..0XXX00000000XXX0.."
".0XX00........00XX0."
"0XX0............0XX0"
"0X0..............0X0"
"0X0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0..............0XX"
"XX0000000000000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XX0000000000000000XX"
"000..............000"
"....................",

"...................."
"XXXXXX0........0XX0."
"0XXXX0..........0XX0"
"0XX00............0X0"
"0X0..............0XX"
"XX0..............00X"
"XX0..............00X"
"X0...............00X"
"X0...............00X"
"XX0..............00X"
"XX0..............0XX"
"0XX0............00X0"
".0X00..........00X0."
".0XXX0000000000XX0.."
"..00XXXXXXXXXXXX0..."
"....000000000000...."
"....................",

"..............00...."
"............00XX00.."
"...000000..0XXXXXX00"
"..0XXXXXX00XX0000XX0"
".0XX00XXXXXX0....0X0"
"0XX0..000XX0.....0XX"
"0X0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"XX0.....0XX0.....0XX"
"XX0000000XX0000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXX"
"XX0000000000000000XX"
"XX0..............0XX"
"XX0..............0XX"
"00................00",

"...................."
".................000"
".................0XX"
"..............000XXX"
"............00XXXXXX"
".........000XXX000XX"
".....0000XXXXX0..0XX"
"..000XXXXX00XX0...0X"
"00XXXX0000.0XX0....0"
"XXXX00.....0XX0....."
"XX00.......0XX0....."
"XXXX000....0XX0....."
"XX0XXXX000.0XX0..000"
"XX000XXXXX00XX0..0XX"
"XX0..0000XXXXX00.0XX"
"XX0......000XXXX00XX"
"0X0.........000XXXXX"
".0.............00XXX"
".................0XX"
"..................00"

};

char *tnr[N_TNR] =
{

"0..............00000"
"X00.............XXXX"
"XXX0..............XX"
"XXXX00............0X"
"XXXXXX0...........0X"
"X000XXX0..........0X"
"X0..0XXX00........0X"
"XX...00XXX0.......0X"
"X0.....0XXX00.....0X"
"XX......0XXXX0....0X"
"X0.......00XXX00..0X"
"XX.........0XXXX0.0X"
"X0..........00XXX00X"
"XXX...........0XXXXX"
"XXX00..........00XXX"
".................0XX"
"..................00",

"X..................."
"X0.................."
"X00................."
"XXX00..............."
"XX0XX0.............."
"XX.0XX00............"
"X...00XX0..........."
"......00X0.........0"
"........0XX0......0X"
".........00X0000000X"
"........00XXXXXXXXXX"
".......0XXX00000000X"
"0....00XXX0........X"
"X...0XXX00.........0"
"X000XXX0............"
"XXXXX00............."
"XXXX0..............."
"XX00................"
"X0.................."
"X0.................."
"0...................",

"0..................0"
"X0.................X"
"XX0..............00X"
"XX00............0XXX"
"X0XX00.........0XXXX"
"0..0X00......00XXX0X"
"0...0XX0....0XXX00.0"
"......0X0.00XXX0...0"
".......XXXXXX00....."
".......0XXXX0......."
"0....00XXX0XX......."
"X...0XXX00.0X0......"
"X000XXX0....0XX0...."
"XXXXX00......00X0000"
"XXXX0.........00X00X"
"XXX0............0XXX"
"XX0..............0XX"
"X0................0X"
"0..................X"
"0..................0",

"0..................."
"X0.................."
"XX0................."
"XXXX00.............."
"X000XXX0............"
"0...00XXXX0........."
"0......00XXXX0......"
"..........00XXXX0..."
".............00XXX00"
"............00XXXXX0"
"..........00XXXXX000"
"0......000XXXX000..."
"00..000XXXXX00......"
"XX00XXXXX000........"
"XXXXXXXX0..........."
"XXX00000XXX0........"
"XX0......0XXXX0....."
"00..........0XXXX0.."
"0.............0XXXX0"
"...........00XXXXXX0"
"0.......00XXXXX000.."
"0....00XXXXXX00....."
"X000XXXXXX000......."
"XXXXXXX000.........."
"XXXX000............."
"XX00................"
"00.................."
"0...................",

"00.................."
"XX0................."
"XX000..............."
"XXXXXX00............"
"00..0XXX00.........."
"0.....00XXX0........"
"........00XXX0......"
"...........00XXX0..."
".............00XXX00"
"..............0XXXX0"
"...........000XXXXX0"
".........000XXX000.."
"0.....000XXXX00....."
"00..00XXXXX00......."
"XX000XXX000........."
"XXXXXX00............"
"XXXX00.............."
"XX00................"
"X0.................."
"0...................",

"0..................."
"X..................."
"XX0XX0XXX0XX0XX....."
"XXX0XXX0XXX0XXXXXX.."
"XX.............XXXX."
"0................XXX"
"0.................XX"
"..................XX"
"..................0X"
"..................0X"
"..................0X"
"..................0X"
"0.................XX"
"0.................XX"
"X0..............0XXX"
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXX."
"XXXXXXXXXXXXXXXXX..."
"X0.................."
"0..................."
"0...................",

"00000..............."
"XXX00..............."
"XX0................."
"X0.................."
"XX.................."
"XX.................0"
"X0.................X"
"XX0000000000000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XX000000000000000XXX"
"X0................XX"
"XX.................X"
"X0.................0"
"XX.................."
"X0.................."
"XXX................."
"X0XX0...............",

".............0000..."
"XXXXX00....00XXXX00."
"XXXX000....0XXX00X00"
".XXX......0XXX00.0XX"
"XXX......0XXX0....XX"
"XX.......0XX0.....0X"
"XX......0XXX0.....0X"
"X0......0XX0......0X"
"X0.....0XXX.......0X"
"XX....0XXX0.......0X"
"XXX..0XXX0........XX"
".0XX0XXX0........0XX"
"..0XXXX0......0XXXX0"
"....000.......000000",

"...................0"
"...................0"
"..................0X"
"....000..........0XX"
"..00XXX00.......0XXX"
".0XXXXXXX0....00XX00"
".0XX000XX0...0XXX0.."
"XXX0...00XX.0XXX0..."
"XX0......0X0XX00...."
"X0.......0XXX0......"
"XX........XX0......."
"X0........X0.......0"
"XX........X........X"
"XX0000000XXX000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XX00000000000000XXXX"
"XX................XX"
"X..................X"
"0..................0",

"...000000..........."
"..0XXXXXX0.........."
".0XXXXXXXX0........."
".XXX0000XX0........."
"0XX0....0XX........."
"XX0......0X0........"
"0X.......0X0........"
"X0.......0X0.......0"
"XX.......0X0.......0"
"XX00000000X00000000X"
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXX"
"X000000000000000000X"
"0..................0"
"0..................0",

"...................."
"......XXXXXXXXX....."
"....0XXXXXXXXXXX0..."
"...0XXXXXXXXXXXXX0.."
"..0XXXXXXXXXXXXXXX0."
".0XXX...........XXX."
"0XX..............XX0"
"XX................XX"
"XX................0X"
"X0................0X"
"X0................0X"
"X0................0X"
"XX................0X"
"XX................XX"
"0XX..............XXX"
".0XXX...........XXX0"
"..0XXXXXXXXXXXXXXX0."
"...0XXXXXXXXXXXXX0.."
"....0XXXXXXXXXXX0..."
".....0XXXXXXXX0....."
".........000........",

"0..................."
"0..................."
"0X000000000000000000"
"0XXXXXXXXXXXXXXXXXX0"
"0XXXXXXXXXXXXXXXXXX0"
"0X000000000000XXX00."
"0...........0XXX0..."
"...........0XXX0...."
"..........0XXX0....."
".........0XXX0......"
".......0XXX00......."
"......0XXX0........."
".....0XXX0.........."
"....0XXX0..........."
"..0XXXX0...........0"
".0XXXX00000000000000"
"0XXXXXXXXXXXXXXXXXX0"
"0XXXXXXXXXXXXXXXXXX0"
"0X0000000000000000X0"
"00.................0"
"0..................0",

"0..................0"
"0X.................0"
"0X000000000000000000"
"0XXXXXXXXXXXXXXXXXX0"
"0XXXXXXXXXXXXXXXXXX0"
"000XXX000000000000X0"
"...0XX0............0"
".....0XX0..........0"
".......0XXX0........"
".........0XXX0......"
"...........0XXX0...."
"..............0XX0.."
"..............0XXXX0"
"............00XXXXX0"
"..........00XXXX00.."
"........00XXXX00...."
"......00XXXX00......"
"....00XXXX00........"
".000XXXX00.........0"
"0XXXXX00...........0"
"0XXXX000000000000XX0"
"0XXXXXXXXXXXXXXXXXX0"
"0XXXXXXXXXXXXXXXXXX0"
"0X000000000000000000"
"0..................0",

"...............00000"
"................00XX"
"..................XX"
"..................XX"
"..................0X"
"..................0X"
"..................0X"
"..................0X"
"0.................0X"
"0.................0X"
"0X................XX"
"0X00X0000X00000X00XX"
"0XXXXXXXXXXXXXXXXXXX"
"0X0000X00000X00000XX"
"0X................XX"
"0..................X"
"0..................0",

"...................0"
"...................0"
"0.................00"
"00...............0X0"
"00..............0XX0"
"0X0............0XXX0"
"0XX0..........0XXX00"
"000X0........0XXX0.0"
"0..0X0......0XXX0..0"
"....0X0....0XXX0...."
".....0X0..0XXX0....."
"......0X0XXXX0......"
"0......0XXXX0......0"
"00......0XX0.......0"
"000000000XX000000000"
"0XXXXXXXXXXXXXXXXXX0"
"0XXXXXXXXXXXXXXXXXX0"
"00000000000000000000"
"00.................0"
"0..................0",

"0..................."
"0..................."
"0X.................."
"0X0XX0XXX0XXXX0XXX.."
"0XXXXXXXXXXXXXXXXXX."
"0XXXXXX0XXX0XXX0XXXX"
"0X................0X"
"0.................0X"
"0................XXX"
".................XX0"
".................000",

"...................."
"0..................0"
"00................0X"
"X000000000000000000X"
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXX"
"X000000000000000000X"
"00................0X"
"0..................0"
"0...................",

"...................."
"0..................0"
"X0.................X"
"XX00000000000000000X"
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXX"
"X00000000XX00000000X"
"X.......0XX0.......X"
"0.......0XX0.......0"
"........0XX0........"
"........0XX0........"
"........0XX0........"
"........0XX0........"
"0.......0XX0.......0"
"00......0XX0.......0"
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXX"
"XX0000000000000000XX"
"X0................0X"
"0..................0"
"0..................0",

".........0.........."
".........X00000000.."
"XXXXXX...XXXXXXXXX00"
".XXX.....XXXXXXXXXXX"
".XX......XXXXXXXXXXX"
"XX.......XX.......0X"
"XX.......X........0X"
"X0.......X........0X"
"X0................0X"
"X0................0X"
"XX................0X"
"XX0...............XX"
"XX0...............XX"
".XX..............0XX"
".XXX0..........0XXXX"
"..0XXX00....00XXXX0."
"...0XXXX0000XXXXX0.."
"....00XXXXXXXXXX0..."
"......00XXXXXX00...."
"........00000.......",

"XXXX0..............."
"XXX0................"
"XX0................."
"XX....0XXXXX0......."
"XX......XXX0........"
"XX......0XX0........"
"XX......0XX0........"
"XX......0XX0.......0"
"XX......0XX0.......0"
"XX0000000XX000000000"
"XXXXXXXXXXXXXXXXXXX0"
"XXXXXXXXXXXXXXXXXXX0"
"X0000000000000000000"
"X..................0"
"0..................0",

"...............00..."
"...............0XXXX"
"XXX00............XXX"
"XX0...0XXXXX0.....XX"
"XX0.....XXXX......0X"
"XX0.....0XX0......0X"
"XX0.....0XX0......0X"
"XX0.....0XX0......0X"
"XX0.....0XX0......0X"
"XX0.....0XX0......0X"
"XX0.....0XX0......0X"
"XXX000000XX0000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XXX000000000000000XX"
"000...............00"
"0..................0"
"0..................0",

"...................."
".....00XXXXXX00....."
"....0XXXXXXXXXX00..."
"...0XXXXXXXXXXXXX0.."
"..0XXX00000000XXXX0."
".0XX00.........0XXX."
".XX0............0XXX"
".XX..............0XX"
"XX0...............XX"
"XX................XX"
"XX................0X"
"XX................0X"
"XX................0X"
"XX................0X"
"XX................XX"
"XXXXXXXXXXXXXXXXXXXX"
"XXXXXXXXXXXXXXXXXXXX"
"XX00000000000000000X"
"00.................0"
"0..................0",

"...................."
"XXXXXXX........XX..."
"XXXXXX..........XX.."
".XXX.............XX."
".XX...............XX"
"XX................0X"
"XX................0X"
"X0................0X"
"X0................0X"
"X0................0X"
"X0................XX"
"XX................XX"
"XX0..............XXX"
".XXX0...........0XXX"
"..XXXXX0.....0XXXXX."
"...XXXXXX000XXXXXX.."
"....XXXXXXXXXXXXX..."
".....XXXXXXXXXXX...."
"........00000.......",

"...................."
"............000000.."
"...000000..0XXXXXX0."
"..0XXXXXXX.XXXXXXXXX"
".0XX000XXXXXX0000XXX"
".XXX...0XXXX0....0XX"
"XXX......XX0......0X"
"XX.......XX.......0X"
"XX.......XX.......0X"
"XX.......XX.......0X"
"XX.......XX.......0X"
"XX.......XX.......0X"
"XX.......XX.......XX"
"XX0000000XX0000000XX"
"XXXXXXXXXXXXXXXXXXXX"
"XX00000000000000000X"
"XX.................X"
"X..................X"
"0...................",

"...................0"
"...................0"
"..................00"
"................00X0"
"..............00XXX0"
"...........00XXXXX00"
".........00XXXXX00.0"
".......00XXXXXX0...0"
"....00XXXXX0XX......"
"..00XXXX000.XX......"
"0XXXXX00....XX......"
"0XXXXX......XX......"
"...0XXX0....XX......"
".....0XXX00.XX......"
".......0XXXXXX......"
"..........0XXX00...0"
"............0XXX00.0"
"...............0XX00"
".................0X0"
"...................0"
"...................0"
};



double dist(struct Point p1, struct Point p2)

{
	return sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
}

/*
int round(double v)
{

	double d = floor(v);

	if(v-d > 0.5) return d+1;

	else return d;

}
*/
 

void extractRectangle()

{

	int i, j, real_i, real_j;
	double scale;

	double start_x = (rdata.sgn*rdata.angle*rdata.min_My + rdata.min_Mx)/(1.0+pow(rdata.angle,2));

	double start_y = -rdata.sgn*rdata.angle*start_x + rdata.min_My;

 

	rdata.w = ceil((rdata.max_Mx-rdata.min_Mx)*cos(atan(rdata.angle)));

	rdata.h = ceil((rdata.max_My-rdata.min_My)*cos(atan(rdata.angle)));

 

	scale = (double)HEIGHT/MIN(rdata.w, rdata.h);

	if(rdata.w < rdata.h) {
		rdata.w = HEIGHT;
		rdata.h = rdata.h*scale;
		rdata.h += 2;
		for(i = 1; i < rdata.h-1; i++) {
			
			for(j = 0; j < rdata.w; j++) {
				rgn[0][j] = '.';
				rgn[rdata.h-1][j] = '.';
				
				real_i = round(start_y + i*cos(atan(rdata.angle))/scale - rdata.sgn*j*sin(atan(rdata.angle))/scale);

				real_j = round(start_x + j*cos(atan(rdata.angle))/scale + rdata.sgn*i*sin(atan(rdata.angle))/scale);

				rgn[i][j] = s[real_i][real_j];

			}

		}

	}

	else {

		rdata.h = rdata.w*scale;
		rdata.w = HEIGHT;
		rdata.h += 2;

		for(i = 0; i < rdata.w; i++) {
			rgn[0][i] = '.';
			rgn[rdata.h-1][i] = '.';
			for(j = 1; j < rdata.h-1; j++) {

				real_i = round(start_y + i*cos(atan(rdata.angle))/scale - rdata.sgn*j*sin(atan(rdata.angle))/scale);

				real_j = round(start_x + j*cos(atan(rdata.angle))/scale + rdata.sgn*i*sin(atan(rdata.angle))/scale);

				rgn[rdata.h-j-1][i] = s[real_i][real_j];

			}

		}

	}
}

 

void outputRectangle()
{

	int i, j;

	for(i = 0; i < rdata.h; i++) {

		for(j = 0; j < rdata.w; j++)

			printf("%c", rgn[i][j]);

		printf("\n");

	}

}

 

void outputAll()

{

	FILE *F = fopen("filtered_data.txt", "w");
	int i, j;

	for(i = 0; i < INPUT_H; i++) {

		for(j = 0; j < INPUT_W; j++)
			fprintf(F, "%c", s[i][j]);

		fprintf(F, "\n");

	}

	fclose(F);

}

 

void readData()

{
	int i, j;

	for(i = 0; i < INPUT_H; i++)
		for(j = 0; j < INPUT_W; j++)
			do {
				fscanf(f, "%c", &s[i][j]);
			} while(s[i][j] != '.' && s[i][j] != 'X');
}

 

void prepareData()

{
	int i, j, sgn, a, b, k;
	
	for(i = 0; i < INPUT_H; i++) s[i][0] = s[0][i] = s[i][INPUT_W-1] = s[INPUT_H-1][i] = '.';
 	
	corners[TOP].y = corners[LEFT].x = INF;
	corners[BOTTOM].y = corners[RIGHT].x = 0;
	corners[TOPLEFT].y = corners[TOPLEFT].x = INF;
	corners[BOTRIGHT].y = corners[BOTRIGHT].x = 0;
	corners[TOPRIGHT].y = INF;
	corners[TOPRIGHT].x = 0;
	corners[BOTLEFT].y = 0;
	corners[BOTLEFT].x = INF;

	for(i = 1; i < INPUT_H-1; i++) {

		for(j = 1; j < INPUT_W-1; j++) {

			if(s[i][j] == '.') continue;

			k = 0;

			for(a = -1; a <= 1; a++)
				for(b = -1; b <= 1; b++)
					k = k + (s[i+a][j+b] == 'X');

			if(k <= 3) s[i][j] = '.';

			if(s[i][j] == 'X') {

				if(i < corners[TOP].y) {
					corners[TOP].x = j;
					corners[TOP].y = i;
				}

				if(j > corners[RIGHT].x) {
					corners[RIGHT].x = j;
					corners[RIGHT].y = i;
				}

				if(i > corners[BOTTOM].y) {
					corners[BOTTOM].y = i;
					corners[BOTTOM].x = j;
				}

				if(j < corners[LEFT].x) {
					corners[LEFT].x = j;
					corners[LEFT].y = i;
				}
 				

				if(i+j < corners[TOPLEFT].y+corners[TOPLEFT].x) {
					corners[TOPLEFT].x = j;
					corners[TOPLEFT].y = i;
				}

				if(i+j > corners[BOTRIGHT].y+corners[BOTRIGHT].x) {
					corners[BOTRIGHT].x = j;
					corners[BOTRIGHT].y = i;
				}

				if(i-j > corners[BOTLEFT].y-corners[BOTLEFT].x) {
					corners[BOTLEFT].x = j;
					corners[BOTLEFT].y = i;
				}

				if(i-j < corners[TOPRIGHT].y-corners[TOPRIGHT].x) {
					corners[TOPRIGHT].x = j;
					corners[TOPRIGHT].y = i;
				}

			}

		}

	}
 

	min_Sq = INF;

	for(i = 0; i < 7; i++) {

		for(j = i+1; j < 8; j++) {

			if(dist(corners[i],corners[j]) < 10) continue;
 			

			if(abs(corners[i].x-corners[j].x) <= abs(corners[i].y-corners[j].y)) {
				angle = (corners[i].x-corners[j].x)/(double)(corners[i].y-corners[j].y);
				sgn = 1;
			}

			else {

				angle = (corners[i].y-corners[j].y)/(double)(corners[i].x-corners[j].x);
				sgn = -1;
			}
 			

			min_Mx = min_My = INF;

			max_Mx = max_My = -INF;

			for(k = 0; k < 8; k++) {

				My = corners[k].y + sgn*corners[k].x*angle;

				if(My < min_My) {
					min_My = My;
				}

				if(My > max_My) {
					max_My = My;
				}

 

				Mx = corners[k].x - sgn*corners[k].y*angle;

				if(Mx < min_Mx) {
					min_Mx = Mx;
				}

				if(Mx > max_Mx) {
					max_Mx = Mx;
				}

			}

			Sq = (max_Mx-min_Mx)*(max_My-min_My)*cos(atan(angle))*cos(atan(angle));

			if(Sq < min_Sq) {

				min_Sq = Sq;
				rdata.angle = angle;
				rdata.min_Mx = min_Mx;
				rdata.max_Mx = max_Mx;
				rdata.min_My = min_My;
				rdata.max_My = max_My;
				rdata.sgn = sgn;

			}

		}

	}

 

	extractRectangle();

}

 

unsigned int mem[400];

char res[7];

char opt_res[7];

int minm, opt_m;


void adjust(int offset, int mismatches, int letters)
{

	int m, i, j, l, a, opta, optm, m1, n;

	if(mismatches > MIN(opt_m,minm)) return;

	if(offset + 4 >= rdata.h) {
		while(offset < rdata.h) {
			for(i = 0; i < HEIGHT; i++) mismatches = mismatches+(rgn[offset][i] != '.');
			if(mismatches > MIN(opt_m,minm)) return;
			offset++;
		}
	}


	if(offset >= rdata.h && mismatches < MIN(opt_m,minm)) {
		minm = mismatches;
		res[letters] = 0;
		strcpy(opt_res, res);
		return;
	}

	if(mem[offset/4] < mismatches) return;

	mem[offset/4] = mismatches;

	if(mismatches > THRESHOLD*offset*HEIGHT) return;

 

	if(letters >= 6) return;

 

	for(l = 0; l < N; l++) {

		m = mismatches;

		optm = INF;

		n = strlen(let[l])/HEIGHT;

		
		for(a = -2; a < 6; a++) {

			m1 = 0;

			for(i = 0; i < HEIGHT; i++) {
				for(j = 0; j < n; j++) {

					if(a + offset + j > rdata.h) m1 = m1 + ('X' == let[l][j*HEIGHT+i]);
					else if(rgn[a+offset+j][i] == 'X' && let[l][j*HEIGHT+i] == '.') m1++;
					else if(rgn[a+offset+j][i] == '.' && let[l][j*HEIGHT+i] == 'X') m1++;
				}

			}

			if(m1+m < optm) {
				optm = m1+m;
				opta = a;
			}
			
//			if(m1 < 0.1*n*HEIGHT) break;

 		
			if(a < 0) continue;
			for(i = 0; i < HEIGHT; i++) m = m+(rgn[offset+a][i] != '.');
			
		}
		

		res[letters] = let_val[l];

		adjust(offset+n+opta, optm, letters+1);
	}
}

void adjust1(int offset, int mismatches, int letters)
{
	int m, i, j, l, a, opta, optm, m1, n;

	if(mismatches > MIN(opt_m,minm)) return;

	if(offset + 4 >= rdata.h) {
		while(offset < rdata.h) {
			for(i = 0; i < HEIGHT; i++) mismatches = mismatches+(rgn[offset][i] != '.');
			if(mismatches > MIN(opt_m,minm)) return;
			offset++;
		}
	}


	if(offset >= rdata.h && mismatches < MIN(opt_m,minm)) {
		minm = mismatches;
		res[letters] = 0;
		strcpy(opt_res, res);
		return;
	}

	if(mem[offset/4] < mismatches) return;

	mem[offset/4] = mismatches;

	if(mismatches > THRESHOLD*offset*HEIGHT) return;

 

	if(letters >= 6) return;

 

	for(l = 0; l < N; l++) {

		m = mismatches;

		optm = INF;

		n = strlen(let[l])/HEIGHT;

		
		for(a = -2; a < 6; a++) {

			m1 = 0;

			for(i = 0; i < HEIGHT; i++) {
				for(j = 0; j < n; j++) {



					if(a + offset + j > rdata.h) m1 = m1 + ('X' == let[l][(n-j)*HEIGHT-i-1]);
					else m1 = m1 + (rgn[a+offset+j][i] != let[l][(n-j)*HEIGHT-i-1]);

				}

			}

			if(m1+m < optm) {
				optm = m1+m;
				opta = a;
			}
			
			//if(m1 < 0.1*n*HEIGHT) break;

 		
			if(a < 0) continue;
			for(i = 0; i < HEIGHT; i++) m = m+(rgn[offset+a][i] != '.');
			
		}
		

		res[letters] = let_val[l];

		adjust1(offset+n+opta, optm, letters+1);
	}

}

char recognized[10];

void recognize()

{

	int i, l;
	opt_m = INF;

	recognized[0] = 0;


	memset(mem, 0xFE, sizeof(mem));

	let = arial;
	let_val = vals_arial;
	N = N_AR;
	minm = INF;
	adjust(0, 0, 0);
//	printf("%d\n", minm);
	if(minm < opt_m) {
		opt_m = minm;
		l = strlen(opt_res);
		for(i = l-1; i >= 0; i--) recognized[l-i-1] = opt_res[i];
		recognized[l] = 0;
	}
	
	minm = INF;
	adjust1(0, 0, 0);
//	printf("%d\n", minm);
	if(minm < opt_m) {
		opt_m = minm;
		l = strlen(opt_res);
		for(i = l-1; i >= 0; i--) recognized[i] = opt_res[i];
		recognized[l] = 0;
	}
	
	memset(mem, 0xFE, sizeof(mem));

	let = courier;
	let_val = vals_courier;
	N = N_COUR;
	minm = INF;
	adjust(0, 0, 0);
//	printf("%d\n", minm);
	if(minm < opt_m) {
		opt_m = minm;
		l = strlen(opt_res);
		for(i = l-1; i >= 0; i--) recognized[l-i-1] = opt_res[i];
		recognized[l] = 0;
	}
	
	minm = INF;
	adjust1(0, 0, 0);
//	printf("%d\n", minm);
	if(minm < opt_m) {
		opt_m = minm;
		l = strlen(opt_res);
		for(i = l-1; i >= 0; i--) recognized[i] = opt_res[i];
		recognized[l] = 0;
	}
	
	memset(mem, 0xFE, sizeof(mem));
	
	let = tnr;
	let_val = vals_tnr;
	N = N_TNR;
	minm = INF;
	adjust(0, 0, 0);
//	printf("%d\n", minm);
	if(minm < opt_m) {
		opt_m = minm;
		l = strlen(opt_res);
		for(i = l-1; i >= 0; i--) recognized[l-i-1] = opt_res[i];
		recognized[l] = 0;
	}
	
	minm = INF;
	adjust1(0, 0, 0);
//	printf("%d\n", minm);
	if(minm < opt_m) {
		opt_m = minm;
		l = strlen(opt_res);
		for(i = l-1; i >= 0; i--) recognized[i] = opt_res[i];
		recognized[l] = 0;
	}

	l = strlen(recognized);
	if(l == 0) {
		printf("XXX\n");
		return;
	}
	
	printf("%s\n", recognized);

}

 

int main()
{

	int n, n_tests;

	//f = fopen("test3.txt", "r");
	f = stdin;

	fscanf(f, "%d", &n_tests);

	for(n = 0; n < n_tests; n++) {

		readData();

		prepareData();

		//outputRectangle();

		//outputAll();

		recognize();
		//printf("XXX\n");

	}

	return 0;
}