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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <bits/stdc++.h>

constexpr int MAX_SIZE = 2'004;
constexpr int ALPHABET = 26;

int n, m;

std::set<int> row;
std::set<int> col;

struct FastGrid {
    char grid[MAX_SIZE][MAX_SIZE];

    int row_color_count[MAX_SIZE][ALPHABET];
    int col_color_count[MAX_SIZE][ALPHABET];

    FastGrid(int n, int m) {
        for (int i = 0; i < n; ++i) {
            row.insert(i);
        }
        for (int i = 0; i < m; ++i) {
            col.insert(i);
        }

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                grid[i][j] = ' ';
            }
        }

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

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

    bool is_empty() {
        return row.empty() && col.empty();
    }

    char get_row_color(int r) {
        for (int i = 0; i < ALPHABET; ++i) {
            if (row_color_count[r][i] > 0) {
                return i + 'A';
            }
        }
        return ' ';
    }

    char get_col_color(int c) {
        for (int i = 0; i < ALPHABET; ++i) {
            if (col_color_count[c][i] > 0) {
                return i + 'A';
            }
        }
        return ' ';
    }

    bool is_row_one_color(int r) {
        int count = 0;
        for (int i = 0; i < ALPHABET; ++i) {
            if (row_color_count[r][i] > 0) {
                count++;
            }
        }
        return count == 1;
    }

    int how_many_colors_in_row(int r) {
        int count = 0;
        for (int i = 0; i < ALPHABET; ++i) {
            if (row_color_count[r][i] > 0) {
                count++;
            }
        }
        return count;
    }

    bool is_col_one_color(int c) {
        int count = 0;
        for (int i = 0; i < ALPHABET; ++i) {
            if (col_color_count[c][i] > 0) {
                count++;
            }
        }
        return count == 1;
    }

    int how_many_colors_in_col(int c) {
        int count = 0;
        for (int i = 0; i < ALPHABET; ++i) {
            if (col_color_count[c][i] > 0) {
                count++;
            }
        }
        return count;
    }

    void insert(int r, int c, char color) {
        grid[r][c] = color;
        row_color_count[r][color - 'A']++;
        col_color_count[c][color - 'A']++;
    }

    void remove_row(int r) {
        for (int i = 0; i < m; ++i) {
            if (grid[r][i] != ' ') {
                col_color_count[i][grid[r][i] - 'A']--;
            }
            grid[r][i] = ' ';
        }
    }

    void remove_col(int c) {
        for (int i = 0; i < n; ++i) {
            if (grid[i][c] != ' ') {
                row_color_count[i][grid[i][c] - 'A']--;
            }
            grid[i][c] = ' ';
        }
    }

    void print() {
        std::cout << "PRINTING GRID: " << std::endl;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (grid[i][j] != ' ') {
                    std::cout << grid[i][j] << " ";
                }
                else {
                    std::cout << "  ";
                }
            }
            std::cout << std::endl;
        }
        std::cout << std::endl;
    }
    
};

std::vector<std::tuple<char, int, char>> output;


int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cout.tie(NULL);

    std::cin >> n >> m;
    FastGrid T(n, m);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; ++j) {
            char tmp;
            std::cin >> tmp;
            T.insert(i, j, tmp); 
        }
    }

    int count = 0;
    while (!T.is_empty() && count < n + m) {
        std::vector<int> to_remove;
        for (int r : row) {
            if (T.is_row_one_color(r)) {
                T.remove_row(r);
                output.push_back(std::make_tuple('R', r, T.get_row_color(r)));
                to_remove.push_back(r);
            }
        }

        for (auto r : to_remove) {
            row.erase(r);
        }
        to_remove.clear();

        for (int c : col) {
            if (T.is_col_one_color(c)) {
                T.remove_col(c);
                output.push_back(std::make_tuple('K', c, T.get_col_color(c)));
                to_remove.push_back(c);
            }
        }

        for (auto c : to_remove) {
            col.erase(c);
        }

        count++;
    }

    std::cout << output.size() << std::endl;
    for (int i = output.size() - 1; i >= 0; --i) {
        std::cout << std::get<0>(output[i]) << " " << std::get<1>(output[i]) + 1 << " " << std::get<2>(output[i]) << std::endl;
    }

    
    return 0;
}