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

#define SIZE 60
#define BORDER 2

#define ERUPTION 100

#define WSIZE 5
#define WBASE 2

#define MAXP 13

#define MAXG (1<<MAXP)

#define RAND 1

int vpos[SIZE][SIZE];
int badness[SIZE][SIZE];
int probes[SIZE][SIZE];
int ash[2][SIZE][SIZE][SIZE][SIZE];

int wind[WSIZE][WSIZE];

int gsize[MAXG];

int X,Y,K,T=0;

void pprobe(int x,int y)
{
  printf("%d %d\n",x,y); fflush(stdout);
}

void init()
{
  int x,y,xx,yy;
  printf("Paste input file:\n"); fflush(stdout);
  scanf("%d %d %d",&X,&Y,&K);
  for(x=0;x<X;x++)for(y=0;y<Y;y++)
  {
    vpos[x][y]=((x<BORDER || x>=X-BORDER || y<BORDER || y>=Y-BORDER) ? 0 : 1);
    for(xx=0;xx<X;xx++)for(yy=0;yy<Y;yy++)ash[T][x][y][xx][yy]=0;
  }
  if(K>MAXP)K=MAXP;
}

int probebadness(int px,int py,int p)
{
  int i,x,y,m;
  for(i=0;i<MAXG;i++)gsize[i]=0;
  for(x=0;x<X;x++)for(y=0;y<Y;y++)if(vpos[x][y])
  {
    m=0;
    if(ash[T][x][y][px][py])m=1<<p;
    gsize[m|(vpos[x][y]>>1)]++;
  }
  m=0;
  for(i=0;i<MAXG;i++)if(gsize[i]>m)m=gsize[i];
  return 4*m+2*!vpos[px][py]+!!probes[px][py];
}

void addprobe(int px,int py,int p)
{
  int x,y,m;
  for(x=0;x<X;x++)for(y=0;y<Y;y++)if(vpos[x][y])
  {
    m=0;
    if(ash[T][x][y][px][py])m=2<<p;
    vpos[x][y]|=m;
  }
}

void selectprobe(int bb, int bc,int p)
{
  int x,y;
  for(x=0;x<X;x++)for(y=0;y<Y;y++)if(badness[x][y]==bb)
  {
    if(!RAND||(rand()%bc)==0)
    {
      addprobe(x,y,p);
      pprobe(x,y);
      probes[x][y]=1;
      return;
    }
    bc--;
  }
}

void makesubmit()
{
  int x,y,k;

  for(x=0;x<X;x++)for(y=0;y<Y;y++){ vpos[x][y]=!!vpos[x][y]; probes[x][y]=0; }

  printf("Next submit:\n"); fflush(stdout);

  for(k=0;k<K;k++)
  {
    int bb=X*Y*7,b,bc=0;
    for(x=0;x<X;x++)for(y=0;y<Y;y++)
    {
      badness[x][y]=b=probebadness(x,y,k);
      if(b<bb){bb=b; bc=0; }
      if(b==bb)bc++;
    }
    selectprobe(bb,bc,k);
  }
}

void erupt()
{
  int x,y;
  for(x=0;x<X;x++)for(y=0;y<Y;y++)ash[T][x][y][x][y]+=ERUPTION;
}

void probevalue(int px,int py,int v)
{
  int x,y;
  for(x=0;x<X;x++)for(y=0;y<Y;y++)if(!ash[T][x][y][px][py]!=!v)vpos[x][y]=0;
  vpos[px][py]=0;  //dostali sme sa k tomu aby sme nieco vyhodnocovali => nebolo OK => neni tam sopka
}

void simwind()
{
  int x,y,xx,yy,wx,wy,a;
  for(x=0;x<X;x++)for(y=0;y<Y;y++)for(xx=0;xx<X;xx++)for(yy=0;yy<Y;yy++)
  {
    a=0;
    for(wx=0;wx<WSIZE;wx++)for(wy=0;wy<WSIZE;wy++)if(xx-wx+WBASE>=0)if(xx-wx+WBASE<X)if(yy-wy+WBASE>=0)if(yy-wy+WBASE<Y)
    {
      a+=ash[T][x][y][xx-wx+WBASE][yy-wy+WBASE]*wind[wx][wy]/100;
    }
    ash[1-T][x][y][xx][yy]=a;
  }
  T=1-T;
}

void pstatus()
{
  int x,y;
  printf("Volcano position:\n"); fflush(stdout);
  for(y=0;y<Y;y++)
  {
    for(x=0;x<X;x++)printf("%c",vpos[x][y]?'*':'.');
    printf("\n"); fflush(stdout);
  }
}

int processresponse()
{
  int r,x,y,v;
  printf("Paste tester response:\n"); fflush(stdout);
  scanf(" Round: %d",&r);
  while(scanf(" Probe [%d,%d]: %d",&x,&y,&v)==3)probevalue(x,y,v);
  scanf(" Wind:");
  for(y=0;y<WSIZE;y++)for(x=0;x<WSIZE;x++)scanf(" %d",&(wind[x][y]));
  simwind();
  pstatus();
  return 1;
}

int oneturn()
{
  erupt();
  makesubmit();
  return processresponse();
}

int main()
{
  init();
  while(oneturn());
  return 0;
}
