1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <iostream>

using namespace std;

struct PaintAction {
  int count;
  int index;
  char color;
  char type;

  PaintAction(int count, int index, char color, char type) {
    this->count = count;
    this->index = index;
    this->color = color;
    this->type = type;
  }
};

bool compare_paint_actions(PaintAction const& A, PaintAction const& B) {
  return A.count < B.count;
}

int main() {
  int n, m;

  cin >> n >> m;

  vector<vector<char>> board(n);

  for (int i = 0; i < n; i++) {
    string colors;

    board[i].resize(m);

    cin >> colors;

    for (int j = 0; j < m; j++) {
      board[i][j] = colors[j];
    }
  }

  vector<PaintAction> colors_to_paint_map['Z' + 2]['Z' + 2];

  for (char i = 'A'; i <= 'Z'; i++) {
    colors_to_paint_map[i]['K'] = vector<PaintAction>();
    colors_to_paint_map[i]['R'] = vector<PaintAction>();
  }

  for (int i = 0; i < n; i++) {
    int color_counts['Z' + 2];

    for (char j = 'A'; j <= 'Z'; j++) {
      color_counts[j] = 0;
    }

    for (int j = 0; j < m; j++) {
      color_counts[board[i][j]]++;
    }

    for (char j = 'A'; j <= 'Z'; j++) {
      if (color_counts[j]) {
        PaintAction paint_action = PaintAction(
          color_counts[j],
          i,
          j,
          'R'
        );

        colors_to_paint_map[j]['R'].push_back(paint_action);
      }
    }
  }

  for (int i = 0; i < m; i++) {
    int color_counts['Z' + 2];

    for (char j = 'A'; j <= 'Z'; j++) {
      color_counts[j] = 0;
    }

    for (int j = 0; j < n; j++) {
      color_counts[board[j][i]]++;
    }

    for (char j = 'A'; j <= 'Z'; j++) {
      if (color_counts[j]) {
        PaintAction paint_action = PaintAction(
          color_counts[j],
          i,
          j,
          'K'
        );

        colors_to_paint_map[j]['K'].push_back(paint_action);
      }
    }
  }

  for (char i = 'A'; i <= 'Z'; i++) {
    sort(colors_to_paint_map[i]['R'].begin(), colors_to_paint_map[i]['R'].end(), &compare_paint_actions);
    sort(colors_to_paint_map[i]['K'].begin(), colors_to_paint_map[i]['K'].end(), &compare_paint_actions);
  }

  vector<PaintAction> result;
  int removed_from_columns['Z' + 2];
  int removed_from_rows['Z' + 2];

  for (char i = 'A'; i <= 'Z'; i++) {
    removed_from_columns[i] = 0;
    removed_from_rows[i] = 0;
  }

  while (n > 0 || m > 0) {
    for (char color = 'A'; color <= 'Z'; color++) {
      auto& rows = colors_to_paint_map[color]['R'];
      auto& columns = colors_to_paint_map[color]['K'];

      // for (int i = 0; i < colors_to_paint_map['B']['K'].size(); i++) {
      //   printf(
      //     "DEBUG %c: %c %d %c %d\n",
      //     color, colors_to_paint_map['B']['K'][i].type,
      //     colors_to_paint_map['B']['K'][i].index + 1,
      //     colors_to_paint_map['B']['K'][i].color,
      //     colors_to_paint_map['B']['K'][i].count
      //   );
      // }

      if (n > 0 && !rows.empty()) {
        PaintAction action = rows.back();
        int delta = removed_from_rows[color];

        if (action.count - delta >= m) {
          // printf("%c %d %c %d %d\n", action.type, action.index + 1, action.color, action.count, delta);
          n--;
          result.push_back(action);
          removed_from_columns[color]++;
          rows.pop_back();
        }
      }


      if (m > 0 && !columns.empty()) {
        PaintAction action = columns.back();
        int delta = removed_from_columns[color];

        if (action.count - delta >= n) {
          // printf("%c %d %c %d %d\n", action.type, action.index + 1, action.color, action.count, delta);
          m--;
          result.push_back(action);
          removed_from_rows[color]++;
          columns.pop_back();
        }
      }
    }
  }

  printf("%ld\n", result.size());
  for (int i = result.size() - 1; i >= 0; i--) {
    printf("%c %d %c\n", result[i].type, result[i].index + 1, result[i].color);
  }

  return 0;
}