#include <stdio.h>

//files will not have comments in them 
//Each line will have one number on it. 
//So you should read first line to get the file type, 
//read second line to get rows, read third line to get columns
//read fourth line to get max value (usually 15 or 225)
//note: if one file type, we'll be getting 

main()
{

	char pe;
	int pnumber,rowCount,columnCount,maxValue,r,g,b;
	int totalScansNeeded;
	int grayValue;
	int i=0;
	//get file type
	scanf("%c%d",&pe,&pnumber);
	//get row count
	scanf("%d",&rowCount);
	//get column count
	scanf("%d",&columnCount);
	//read in max
	scanf("%d",&maxValue);
	//calculate scans needed
	totalScansNeeded=rowCount*columnCount;

	printf("P2\n");
	printf("%d\n",rowCount);
	printf("%d\n",columnCount);
	printf("%d\n",255);

	//scan all values in
	while(i<totalScansNeeded){
		scanf("%d%d%d", &r, &g, &b);
		grayValue = ((r + g + b) * 255) / (3 * maxValue);
		printf("%d\n",grayValue);
		i++;
	}

}