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
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <iomanip>

//#define __DEBUG

#define REP(x,n) for (int x=0;x<(n);++x)
#define VAR(x,n) __typeof(n) x = (n)
#define FOREACH(x,c) for(VAR(x, (c).begin()); x != (c).end(); ++x)
#define CONTAINS(x,elem) ((x).find(elem) != (x).end())
#ifdef __DEBUG
    #define DEBUG(x) x
#else
    #define DEBUG(x)
#endif

using namespace std;

void solve();

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    solve();
    return 0;
}

#define MAX_N 2001
#define MAX_M 2001

#define COLOR_ANY '@'
#define COLOR_MULTI '$'
#define TYPE_ROW 'R'
#define TYPE_COLUMN 'K'

int n,m;
//string rows[MAX_N];
struct Line {
    int index;
    //vector<char*> elements;
    map<char, int> colorsCount;

    void subtractColor(char color) {
        map<char,int>::iterator iter = colorsCount.find(color);
        if (iter != colorsCount.end()) {
            if (iter->second > 1)
                --iter->second;
            else
                colorsCount.erase(iter);
        }
    }
};
vector<Line> board;
vector<Line> boardRev;

struct PaintColor {
    char color;
    int index;
};
struct ResultPaint {
    char type;
    int index;
    char color;
} result[MAX_N+MAX_M];
int resultSize = 0;
ostream& operator<<(ostream& os, const ResultPaint& rp) {
    return os << rp.type << " " << rp.index << " " << rp.color;
}

void addResult(char type, int index, char color) {
    result[resultSize++] = {type, index, color};
}


char getSingleColor(const Line& line) {
    if (line.colorsCount.size() == 1) {
        return line.colorsCount.begin()->first;
    } else {
        return COLOR_MULTI;
    }
}

void removeLine(vector<Line>& board, vector<Line>& reverseBoard, int i, char color) {
    swap(board[i], board[board.size()-1]);
    board.pop_back();
    for (Line& line : reverseBoard) {
        line.subtractColor(color);
    }
}
void fillSingleColorLines(vector<Line>& board, vector<Line>& reverseBoard, vector<PaintColor>& output) {
    output.clear();
    REP(x, board.size()) {
        Line& line = board[x];
        char color = getSingleColor(line);
        DEBUG(cerr << "  line " << line.index << " has color " << color << endl;)
        if (color != COLOR_MULTI) {
            output.push_back({color, line.index});
            removeLine(board, reverseBoard, x--, color);
        }
    }
}

void solve() {
    cin>>n>>m;
    board.resize(n);
    boardRev.resize(m);
    string input;
    input.reserve(m);
    REP(x,m) {
        boardRev[x].index = x+1;
    }

    REP(x,n) {
        cin>>input;
        board[x].index = x+1;
        REP(y,m) {
            ++board[x].colorsCount[input[y]];
            ++boardRev[y].colorsCount[input[y]];
        }
    }
    vector<PaintColor> buffer;
    buffer.reserve(max(n,m));

    bool hasAnyRemainingLine = true;
    while (hasAnyRemainingLine) {
        DEBUG(
            cerr << "dump for rows:" << endl;
            FOREACH(row, board) {
                FOREACH(it, row->colorsCount) {
                    cerr << " " << it->first << ": " << it->second;
                }
                cerr << endl;
            }
            cerr << "dump for columns:" << endl;
            FOREACH(row, boardRev) {
                FOREACH(it, row->colorsCount) {
                    cerr << " " << it->first << ": " << it->second;
                }
                cerr << endl;
            }
            cerr<<endl;
        )
        hasAnyRemainingLine = false;
        DEBUG(cerr << "### checking rows" << endl;)
        fillSingleColorLines(board, boardRev, buffer);
        if (!buffer.empty()) {
            hasAnyRemainingLine = true;
            FOREACH(it, buffer) {
                DEBUG(cerr << "Paint row " << it->index << " to " << it->color << endl;)
                addResult(TYPE_ROW, it->index, it->color);
            }
        }

        DEBUG(cerr << "### checking columns" << endl;)
        fillSingleColorLines(boardRev, board, buffer);
        if (!buffer.empty()) {
            hasAnyRemainingLine = true;
            FOREACH(it, buffer) {
                DEBUG(cerr << "Paint column " << it->index << " to " << it->color << endl;)
                addResult(TYPE_COLUMN, it->index, it->color);
            }
        }
        
    }

    cout << resultSize << endl;

    ResultPaint* writePtr = result + resultSize;
    REP(x, resultSize) {
        cout << *(--writePtr) << endl;
    }
}