#include <stdio.h>

/*
Use in1.txt and in2.txt as redirected input for this example.
*/

void writeHeader(int rows, int cols){
        fprintf(stdout,"P6\n %d %d 255\n",cols,rows);
}

typedef struct{
	int r;
	int g;
	int b;
}pixel;


void writePixel(pixel q){
        fprintf(stdout,"%c%c%c",q.r,q.g,q.b);
}

int main(void){

        //Declare an image array
        int img[100][100];

        //Load in how many rows and columns
        int nRows;
        int nCols;

        scanf("%d %d",&nRows, &nCols);

        //Load in the picture values
        //for every row
        int row, col;
        for(row = 0; row < nRows; row++){
                //for every column
                for(col = 0; col < nCols; col++){
                        //read in a value
                        int x;
                        scanf("%d",&x);
                        //store it at the right place
                        img[row][col] = x;
                }
        }

	//output the image

	//print the header
	writeHeader(nRows,nCols);
        //for every row
        for(row = 0; row < nRows; row++){
                //for every column
                for(col = 0; col < nCols; col++){
			//change this so that we use a pixel structure
			pixel p;

			switch (img[row][col]){
				case 0:
					p.r = 0;
					p.g = 0;
					p.b = 0;
					break;
				case 1:
					p.r = 255;
					p.g = 0;
					p.b = 0;
					break;
				case 2:
					p.r = 0;
					p.g = 255;
					p.b = 0;
					break;
				case 3:
					p.r = 255;
					p.g = 255;
					p.b = 0;
					break;
				case 4:
					p.r = 0;
					p.g = 0;
					p.b = 255;
					break;
				case 5:
					p.r = 255;
					p.g = 0;
					p.b = 255;
					break;
				case 6:
					p.r = 0;
					p.g = 255;
					p.b = 255;
					break;
				case 7:
					p.r = 255;
					p.g = 255;
					p.b = 255;
					break;
			} 

                        writePixel(p);
                }
        }
	return 0;
}
