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
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
using namespace std;

const int N = 2000 + 9;
const char CHAR0 = 0;

int h;
int w;

class Line {
	int cnt[26];
	int nonZero;
	int sum;
public:
	void insert(char c) {
		c -= 'A';
		if (0 == cnt[c]) {
			++nonZero;
			sum += c;
		}
		++cnt[c];
	}
	void erase(char c) {
		c -= 'A';
		--cnt[c];
		if (0 == cnt[c]) {
			--nonZero;
			sum -= c;
		}
	}
	int count() const { return nonZero; }
	char get() const { return sum + 'A'; }
};

struct Cell {
	int row;
	int col;
	char color;
	Cell* up;
	Cell* down;
	Cell* left;
	Cell* right;
};

struct Cmd {
	char type;
	int pos;
	char color;
};

Cell cell[N][N];
Line row[N];
Line col[N];

#define rowBe(r) cell[r][0]
#define rowEn(r) cell[r][w + 1]
#define colBe(c) cell[0][c]
#define colEn(c) cell[h + 1][c]

void detach(int r, int c) {
	Cell* uu = cell[r][c].up;
	Cell* dd = cell[r][c].down;
	uu->down = dd;
	dd->up = uu;
	Cell* ll = cell[r][c].left;
	Cell* rr = cell[r][c].right;
	ll->right = rr;
	rr->left = ll;
}

deque<Cmd> res;
int rowDone[N];
int colDone[N];
char buf[N];

int main() {
	scanf("%d%d", &h, &w);
	for (int r = 1; r <= h; ++r) {
		scanf("%s", buf + 1);
		for (int c = 1; c <= w; ++c) {
			cell[r][c] = {
				r,
				c,
				buf[c],
				&cell[r - 1][c],
				&cell[r + 1][c],
				&cell[r][c - 1],
				&cell[r][c + 1]
			};
			row[r].insert(buf[c]);
			col[c].insert(buf[c]);
		}
	}
	for (int r = 1; r <= h; ++r) {
		rowBe(r).right = &cell[r][1];
		rowEn(r).left = &cell[r][w];
	}
	for (int c = 1; c <= w; ++c) {
		colBe(c).down = &cell[1][c];
		colEn(c).up = &cell[h][c];
	}

	queue<int> qrow;
	queue<int> qcol;

	for (int r = 1; r <= h; ++r)
		if (1 == row[r].count())
			qrow.push(r);
	for (int c = 1; c <= w; ++c)
		if (1 == col[c].count())
			qcol.push(c);

	while (!qrow.empty() || !qcol.empty()) {
		while (!qrow.empty()) {
			int r = qrow.front();
			qrow.pop();
			Cell* be = &rowBe(r);
			Cell* en = &rowEn(r);
			char color = be->right->color;
			if (color)
				res.push_front({ 'R', r, color });
			while (be->right != en) {
				int c = be->right->col;
				detach(r, c);
				col[c].erase(color);
				if (0 == colDone[c] && 1 == col[c].count()) {
					colDone[c] = 1;
					qcol.push(c);
				}
			}
		}
		while (!qcol.empty()) {
			int c = qcol.front();
			qcol.pop();
			Cell* be = &colBe(c);
			Cell* en = &colEn(c);
			char color = be->down->color;
			if (color)
				res.push_front({ 'K', c, color });
			while (be->down != en) {
				int r = be->down->row;
				detach(r, c);
				row[r].erase(color);
				if (0 == rowDone[r] && 1 == row[r].count()) {
					rowDone[r] = 1;
					qrow.push(r);
				}
			}
		}
	}

	printf("%d\n", res.size());
	for (int i = 0; i < res.size(); ++i)
		printf("%c %d %c\n", res[i].type, res[i].pos, res[i].color);

	return 0;
}