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
#include <bits/stdc++.h>
using namespace std;

constexpr int M = 2e3+7;

char grid[M][M];
int kolor_r[30][M]; //ile tego koloru jest w tym rzedzie
int kolor_k[30][M]; //ile tego koloru jest w tej kolumnie
int ile_r[M]; //ile kolorow w tym rzedzie
int ile_k[M]; //ile kolorow w tej kolumnie

//czy juz zapisalem i-ty wiersz / kolumne
bool done_r[M]; 
bool done_k[M];

int n, m;

queue<tuple<char, int, char>> Q;
vector<tuple<char, int, char>> ans;

void add(char c, int x){
	if(c == 'R'){
		for(int i=0; i<26; i++){
			if(kolor_r[i][x]){
				done_r[x] = 1;
				Q.push(make_tuple(c,x,('A'+i)));
			}
		}
	}
	else{
		for(int i=0; i<26; i++){
			if(kolor_k[i][x]){
				done_k[x] = 1;
				Q.push(make_tuple(c,x,('A'+i)));
			}
		}
	}
}

void init(){
	for(int i=1; i<=n; i++){
		for(int j=1; j<=m; j++){
			int act = grid[i][j]-'A';
			kolor_r[act][i]++;
			if(kolor_r[act][i] == 1) ile_r[i]++;
		}
		if(ile_r[i] == 1) add('R', i);
	}
	
	for(int j=1; j<=m; j++){
		for(int i=1; i<=n; i++){
			int act = grid[i][j]-'A';
			kolor_k[act][j]++;
			if(kolor_k[act][j] == 1) ile_k[j]++;
		}
		if(ile_k[j] == 1) add('K', j);
	}
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	
	cin >> n >> m;
	
	for(int i=1; i<=n; i++){
		for(int j=1; j<=m; j++){
			cin >> grid[i][j];
		}
	}
	
	init();
	
	while(!Q.empty()){
		char c = get<0>(Q.front());
		int nr = get<1>(Q.front());
		ans.push_back(Q.front());
		Q.pop();
		if(c == 'R'){
			for(int j=1; j<=m; j++){
				char act = grid[nr][j]-'A';
				kolor_k[act][j]--;
				if(kolor_k[act][j] == 0) ile_k[j]--;
				if(ile_k[j] == 1 && done_k[j] == 0) add('K', j);
			}
		}
		else{
			for(int i=1; i<=n; i++){
				char act = grid[i][nr]-'A';
				kolor_r[act][i]--;
				if(kolor_r[act][i] == 0) ile_r[i]--;
				if(ile_r[i] == 1 && done_r[i] == 0) add('R', i);
			}
		}
	}
	
	cout << ans.size() << '\n';
	for(int i=ans.size()-1; i>=0; i--) 
		cout << get<0>(ans[i]) << ' ' << get<1>(ans[i]) << ' ' << get<2>(ans[i]) << '\n';
	
	return 0;
}