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
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;

struct oper {
    char rowcol;
    int rcnum;
    int color;
};

vector<oper> moves_processed;
queue<oper> moves_toprocess;

int n, m;
int plansza_cel[2001][2001];

int col_hmu[2001][26];
int row_hmu[2001][26];

bool coldone[2001];
bool rowdone[2001];

void wildcard_rowcol(char rowcol, int rcnum) {
    if (rowcol == 'R') {
        for (int i = 0; i < m; ++i) { // i - column number
            if (coldone[i]) continue;
            int oldcol = plansza_cel[rcnum][i];
            plansza_cel[rcnum][i] = 50;
            if (oldcol != 50) {
                for (int j = 0; j < 26; ++j) {
                    if (j == oldcol) continue;
                    col_hmu[i][j] += 1;
                    if (col_hmu[i][j] == n) {
                        coldone[i] = true;
                        oper operation{'K', i, j};
                        moves_toprocess.push(operation);
                        break;
                    }
                }
            }
        }
    }
    if (rowcol == 'K') {
        for (int i = 0; i < n; ++i) { // i - row number
            if (rowdone[i]) continue;
            int oldcol = plansza_cel[i][rcnum];
            plansza_cel[i][rcnum] = 50;
            if (oldcol != 50) {
                for (int j = 0; j < 26; ++j) {
                    if (j == oldcol) continue;
                    row_hmu[i][j] += 1;
                    if (row_hmu[i][j] == m) {
                        rowdone[i] = true;
                        oper operation{'R', i, j};
                        moves_toprocess.push(operation);
                        break;
                    }
                }
            }
        }
    }
}

int main() {
    scanf("%d %d\n", &n, &m);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            char c;
            scanf("%c", &c);
            plansza_cel[i][j] = int(c - 'A');
        }
        if (i != n-1) scanf("\n");
    }

    /*
     * i - row number
     * j - column number
     */
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            int color = plansza_cel[i][j];
            col_hmu[j][color] += 1;
            row_hmu[i][color] += 1;
//            printf("KOL %d color %c +1\n", j, char(color + 'A'));
//            printf("ROW %d color %c +1\n", i, char(color + 'A'));
            if (i == n-1) {
                for (int k = 0; k < 26; ++k) {
                    if (col_hmu[j][k] == n) {
                        coldone[j] = true;
                        oper operation{'K', j, k};
                        moves_toprocess.push(operation);
                    }
                }
            }
        }
        for (int k = 0; k < 26; ++k) {
            if (row_hmu[i][k] == m) {
                rowdone[i] = true;
                oper operation{'R', i, k};
                moves_toprocess.push(operation);
            }
        }
    }

//    for (int i = 0; i < n; ++i) {
//        for (int j = 0; j < 26; ++j) {
//            if (j != 0 && j != 15) continue;
//            printf("ROW %2d COLOR %c: %d\n", i, char('A'+j), row_hmu[i][j]);
//        }
//    }
//
//    for (int i = 0; i < m; ++i) {
//        for (int j = 0; j < 26; ++j) {
//            if (j != 0 && j != 15) continue;
//            printf("KOL %2d COLOR %c: %d\n", i, char('A'+j), col_hmu[i][j]);
//        }
//    }

    while (!moves_toprocess.empty()) {
        oper nextmove = moves_toprocess.front();
        moves_toprocess.pop();

        wildcard_rowcol(nextmove.rowcol, nextmove.rcnum);

        moves_processed.push_back(nextmove);
    }

    int opnum = moves_processed.size();

    printf("%d\n", opnum);
    for (int i = opnum - 1; i >= 0; --i) {
        oper operation = moves_processed[i];
        printf("%c %d %c\n", operation.rowcol, operation.rcnum + 1, (operation.color + 'A'));
    }

    return 0;
}