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
// Author: Bartek Knapik

#include <cstdio>
#include <queue>

using namespace std;

const int MAX_N = 2000 + 9;

struct Section
{
    int begin, end, color;
    bool operator<(const Section &oth) const { return begin < oth.begin; }
};

struct Line
{
    int line[MAX_N];
    int counter[30];
    bool removed;
    
    void push(int begin, int end, int color)
    {
        counter[color] += end - begin;
        for (int i = begin; i < end; ++i) line[i] = color;
    }
    
    void erase(int pos) { counter[line[pos]]--; }
    
    int is_single()
    {
        int cnt = 0;
        int res;
        for (int i = 0; i < 26; ++i)
            if (counter[i])
            {
                cnt++;
                if (cnt > 1) return -1;
                res = i;
            }
        
        return res;
    }
    
    void remove() { removed = true; }
};

int n, m;
int board[MAX_N][MAX_N];
Line hlines[MAX_N], vlines[MAX_N];
deque<Section> buffer, candidates;
char input[MAX_N];

int main()
{
    scanf("%d%d", &n, &m);
    
    for (int i = 0; i < n; ++i)
    {
        scanf("%s", input);
        for (int j = 0; j < m; ++j) board[i][j] = input[j] - 'A';
    }
    
    for (int i = 0; i < n; ++i)
    {
        int cur_col = board[i][0];
        int begin, _c;
        begin = 0;
        for (int j = 1; j < m; ++j)
        {
            if (board[i][j] != cur_col)
            {
                hlines[i].push(begin, j, cur_col);
                cur_col = board[i][j];
                begin = j;
            }
        }
        hlines[i].push(begin, m, cur_col);
        _c = hlines[i].is_single();
        if (_c != -1)
            candidates.push_back({ 0, i, _c});
    }
    
    for (int j = 0; j < m; ++j)
    {
        int cur_col = board[0][j];
        int begin, _c;
        begin = 0;
        for (int i = 1; i < n; ++i)
        {
            if (board[i][j] != cur_col)
            {
                vlines[j].push(begin, i, cur_col);
                cur_col = board[i][j];
                begin = i;
            }
        }
        vlines[j].push(begin, n, cur_col);
        _c = vlines[j].is_single();
        if (_c != -1)
            candidates.push_back({ 1, j, _c});
    }
    
    while (!candidates.empty())
    {
        int _c;
        Section candidate = candidates.front();
        candidates.pop_front();
        
        if (candidate.begin == 0)
        {
            if (hlines[candidate.end].removed) continue;
            buffer.push_back(candidate);
            int i = candidate.end;
            hlines[i].remove();
            for (int j = 0; j < m; ++j)
            {
                if (vlines[j].removed) continue;
                vlines[j].erase(i);
                _c = vlines[j].is_single();
                if (_c != -1)
                    candidates.push_back({1, j, _c});
            }
        }
        else
        {
            if (vlines[candidate.end].removed) continue;
            buffer.push_back(candidate);
            int j = candidate.end;
            vlines[j].remove();
            for (int i = 0; i < n; ++i)
            {
                if (hlines[i].removed) continue;
                hlines[i].erase(j);
                _c = hlines[i].is_single();
                if (_c != -1)
                    candidates.push_back({0, i, _c});
            }
        }
    }
    
    printf("%d\n", buffer.size());
    
    while (buffer.size())
    {
        Section cmd = buffer.back();
        buffer.pop_back();
        printf("%c %d %c\n", cmd.begin == 0 ? 'R' : 'K', cmd.end + 1, cmd.color + 'A');
    }
    return 0;
}