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
#include <iostream>
#include <vector>
#include <stack>
#include <string>
#include <unordered_set>


using namespace std;

int n, m, ile_wyzerowanych;
vector<string> vec;
unordered_set<int> cleared_rows;
unordered_set<int> cleared_columns;

void remove_row(int row_number) {
	for (int i=0; i<m; i++) {
		//if (vec[row_number][i] != 0) {
			ile_wyzerowanych += vec[row_number][i] != 0;
			vec[row_number][i] = 0;
		//}
		
	}
}

void remove_column(int row_number) {
	for (int i=0; i<n; i++) {
		//if (vec[i][row_number] != 0) {
			ile_wyzerowanych += vec[i][row_number] != 0;
			vec[i][row_number] = 0;
		//}
	}
}

bool is_good_character(char row_character, char character) {
	return row_character == character || character == 0;
}

char is_row_to_remove(int row_number) {//znajdz kolor o ile mozna usunac
	char first_not_null = 0;
	int i=0;
	while (i<n && vec[row_number][i] == 0) {
	//while (cleared_columns.find(i) != cleared_columns.end()){
		i++;
	}
	if (i == n) {
		return first_not_null;
	}
	first_not_null = vec[row_number][i];
	while (i<m) {
		if (!is_good_character(first_not_null, vec[row_number][i])) {
			return 0;
		}
		i++;
	}
	return first_not_null;
}

char is_column_to_remove(int row_number) {//znajdz kolor o ile mozna usunac
	char first_not_null = 0;
	int i=0;
	while (i<n && vec[i][row_number] == 0) {
	//while (cleared_rows.find(i) != cleared_rows.end()) {
		i++;
	}
	if (i == n) {
		return first_not_null;
	}
	first_not_null = vec[i][row_number];
	while (i<n) {
		if (!is_good_character(first_not_null, vec[i][row_number])) {
			return 0;
		}
		i++;
	}
	return first_not_null;
}

bool is_Finished() {
	/*for (int i=0; i<n; i++) {
		for (int j=0; j<m; j++) {
			if (vec[i][j] != 0) {
				return 0;
			}
		}
	}
	return 1;*/
	
	//bool wszystkie_wiersze = cleared_rows.size() >= n;
	//bool wszystkie_kolumny = cleared_columns.size() >= m;
	
	/*if (cleared_columns.size() + cleared_rows.size() >= n+m) {
		return 1;
	} else {
		return 0;
	}*/
	
	if (ile_wyzerowanych >= n*m) {
		return 1;
	} else {
		return 0;
	}
}

int main(int argc, char *argv[])
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

	cin >> n >> m;
	ile_wyzerowanych = 0;
	stack<string> results;
	string kaczka;
	
	for (int i=0; i<n; i++) {
		cin >> kaczka;
		vec.push_back(kaczka);
	}
	
	bool row_finished = false;
	bool column_finished = false;
	
	int tmp_row_or_column;
	char first_character_to_paint;
	string push_result;
	
	while (!is_Finished()) {
		for (int i=0; i<n; i++) {
			if (cleared_rows.find(i) != cleared_rows.end()) {
				continue;
			}
			
			first_character_to_paint = is_row_to_remove(i);
			if (first_character_to_paint) {
				remove_row(i);
				cleared_rows.insert(i);
				tmp_row_or_column = i+1;
				push_result = "R " + to_string(tmp_row_or_column)+ ' ' + first_character_to_paint;
				results.push(push_result);
			}
		}
		
		for (int i=0; i<m; i++) {
			if (cleared_columns.find(i) != cleared_columns.end()) {
				continue;
			}
			
			first_character_to_paint = is_column_to_remove(i);
			if (first_character_to_paint) {
				remove_column(i);
				cleared_columns.insert(i);
				tmp_row_or_column = i+1;
				push_result = "K " + to_string(tmp_row_or_column)+ ' ' + first_character_to_paint;
				results.push(push_result);
			}
		}
	}
    
    cout<<results.size()<<endl;
	while (!results.empty()) {
		cout<<results.top()<<endl;
		results.pop();
	}

    return 0;
}