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
#include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
const int N = 2e3+3;
char tab[N][N];
bool usd[N][N];
bool r[N], k[N];
int rze[N][30], kol[N][30], cntR[N], cntK[N];
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n, m;
	cin >> n >> m;
	for(int i=0; i<n; i++){
		for(int j=0; j<m; j++){
			cin >> tab[i][j];
			char c=tab[i][j];
			if(rze[i][c-'A']==0) cntR[i]++;
			if(kol[j][c-'A']==0) cntK[j]++;
			rze[i][c-'A']++;
			kol[j][c-'A']++;
		}
	}
	queue<pair<int, bool>> Q;
	for(int i=0; i<n; i++){
		if(cntR[i]==1){
			Q.push({i, 0});
		}
	}
	for(int i=0; i<m; i++){
		if(cntK[i]==1){
			Q.push({i, 1});
		}
	}
	vector<pair<char, pair<int, char>>> out;
	while(!Q.empty()){
		int cur = Q.front().st;
		bool dir = Q.front().nd;
		Q.pop();
		if(dir==0){
			if(cntR[cur]==0 || r[cur]) continue;
			char clr='Z';
			for(int i=0; i<26; i++){
				if(rze[cur][i]>0){
					clr='A'+i;
				}
			}
			out.pb({'R', {cur+1, clr}});
			for(int i=0; i<m; i++){
				if(usd[cur][i]==0){
					char c=tab[cur][i];
					kol[i][c-'A']--;
					if(kol[i][c-'A']==0) cntK[i]--;
					if(cntK[i]==1){
						Q.push({i, 1});
					}
					usd[cur][i]=1;
				}
			}
			r[cur]=1;
		}else{
			if(cntK[cur]==0 || k[cur]) continue;
			char clr='Z';
			for(int i=0; i<26; i++){
				if(kol[cur][i]>0){
					clr='A'+i;
				}
			}
			out.pb({'K', {cur+1, clr}});
			for(int i=0; i<n; i++){
				if(usd[i][cur]==0){
					char c=tab[i][cur];
					rze[i][c-'A']--;
					if(rze[i][c-'A']==0) cntR[i]--;
					if(cntR[i]==1){
						Q.push({i, 0});
					}
					usd[i][cur]=1;
				}
			}
			k[cur]=1;
		}
	}
	cout << out.size() << '\n';
	for(int i=out.size()-1; i>=0; i--){
		cout << out[i].st << ' ' << out[i].nd.st << ' ' << out[i].nd.nd << '\n';
	}
	return 0;
}