#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

int m;
bool passed[10][10];
int t[10][10];
vector<int> dx[3], dy[3];
vector<int> vx[3], vy[3];

int ansX[20], ansY[20], ansS[20];

void init(){
  dx[0].push_back(0); dy[0].push_back(0);
  dx[0].push_back(1); dy[0].push_back(0);
  dx[0].push_back(0); dy[0].push_back(1);
  dx[0].push_back(0); dy[0].push_back(-1);
  dx[0].push_back(-1); dy[0].push_back(0);

  dx[1] = dx[0]; dy[1] = dy[0];
  dx[1].push_back(1); dy[1].push_back(1);
  dx[1].push_back(1); dy[1].push_back(-1);
  dx[1].push_back(-1); dy[1].push_back(1);
  dx[1].push_back(-1); dy[1].push_back(-1);

  dx[2] = dx[1]; dy[2] = dy[1];
  dx[2].push_back(2); dy[2].push_back(0);
  dx[2].push_back(0); dy[2].push_back(2);
  dx[2].push_back(0); dy[2].push_back(-2);
  dx[2].push_back(-2); dy[2].push_back(0);
}

void dropPos(int kind){
  vx[kind].clear();
  vy[kind].clear();
  int from = (kind <= 1 ? 1 : 2);
  int to = (kind <= 1 ? 9 : 8);

  for(int i = from; i < to; i++){
    for(int j = from; j < to; j++){
      bool flg = true;

      for(int k = 0; k < dx[kind].size(); k++){
        int nx = j + dx[kind][k];
        int ny = i + dy[kind][k];

        if(t[ny][nx] == 0){
          flg = false;
          break;
        }
      }

      if(flg){
        vx[kind].push_back(j);
        vy[kind].push_back(i);
      }
    }
  }
}

bool check(int stKind, int stI, int rem){
  memset(passed, 0, sizeof(passed));

  for(int kind = stKind; kind >= 0; kind--){
    for(int i = (kind == stKind ? stI : 0); i < vx[kind].size(); i++){
      int x = vx[kind][i];
      int y = vy[kind][i];
      bool flg = true;

      for(int k = 0; k < dx[kind].size(); k++){
        int nx = x + dx[kind][k];
        int ny = y + dy[kind][k];

        if(t[ny][nx] == 0){
          flg = false;
          break;
        }
      }

      if(!flg) continue;

      for(int k = 0; k < dx[kind].size(); k++){
        int nx = x + dx[kind][k];
        int ny = y + dy[kind][k];
        if(!passed[ny][nx]){
          passed[ny][nx] = true;
          rem--;
        }
      }
    }
  }

  return rem == 0;
}

bool drawCheck(int x, int y, int kind){
  for(int i = 0; i < dx[kind].size(); i++){
    int nx = x + dx[kind][i];
    int ny = y + dy[kind][i];
    if(t[ny][nx] == 0) return false;
  }
  return true;
}

int draw(int x, int y, int kind, int add){
  int res = 0;

  for(int i = 0; i < dx[kind].size(); i++){
    int nx = x + dx[kind][i];
    int ny = y + dy[kind][i];
    t[ny][nx] += add;
    if(t[ny][nx] == 0) res++;
  }
  return res;
}

void show(){
  cout << "--\n";
  for(int i = 0; i < 10; i++){
    for(int j = 0; j < 10; j++){
      cout << t[i][j] << " ";
    }
    cout<<endl;
  }
}

bool solve(int cnt, int stKind, int stI, int rem){
  if(cnt == m){
    return rem == 0;
  }

  while(stKind >= 0 && stI == vx[stKind].size()){
    stKind--;
    stI = 0;
  }

  if(stKind == -1) return false;
  if(!check(stKind, stI, rem)) return false;

  if(drawCheck(vx[stKind][stI], vy[stKind][stI], stKind)){
    int tmp = draw(vx[stKind][stI], vy[stKind][stI], stKind, -1);

    ansX[cnt] = vx[stKind][stI];
    ansY[cnt] = vy[stKind][stI];
    ansS[cnt] = stKind + 1;

    if(solve(cnt + 1, stKind, stI, rem - tmp)) return true;

    draw(vx[stKind][stI], vy[stKind][stI], stKind, 1);
  }

  if(solve(cnt, stKind, stI + 1, rem)) return true;

  return false;
}

int main(){
  init();

  while(cin >> m){
    int rem = 0;

    for(int i = 0; i < 10; i++){
      for(int j = 0; j < 10; j++){
        cin >> t[i][j];
        if(t[i][j] != 0){
          rem++;
        }
      }
    }

    for(int i = 0; i < 3; i++){
      dropPos(i);
    }

    bool flg = solve(0, 2, 0, rem);

    if(!flg){ //submit debug
      int div = 3 / 10;
      cout << 10 / div << endl;
    }

    for(int i = 0; i < m; i++){
      cout << ansX[i] << " " << ansY[i] << " " << ansS[i] << endl;
    }
  }
}

Measured memory_bytes: 1224