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
#!/usr/bin/env python3

import sys

import numpy as np


def main():
    n, m = map(int, sys.stdin.readline().split())
    arr = np.array([list(sys.stdin.readline().strip()) for _ in range(n)])
    rownums = np.arange(1, n + 1)
    colnums = np.arange(1, m + 1)
    moves = []

    def color_columns():
        nonlocal arr, colnums
        if arr.size == 0:
            return False
        same_col = np.all(arr == arr[0, :], axis=0)
        if np.any(same_col):
            for c in np.where(same_col)[0]:
                moves.append(f'K {colnums[c]} {arr[0, c]}')
            arr = arr[:, ~same_col]
            colnums = colnums[~same_col]
        return np.sum(same_col) > 0

    def color_rows():
        nonlocal arr, rownums
        if arr.size == 0:
            return False
        same_row = np.all(arr.T == arr[:, 0], axis=0)
        if np.any(same_row):
            for r in np.where(same_row)[0]:
                moves.append(f'R {rownums[r]} {arr[r, 0]}')
            arr = arr[~same_row, :]
            rownums = rownums[~same_row]
        return np.sum(same_row) > 0

    while color_columns() or color_rows():
        pass
    assert arr.size == 0

    print(len(moves))
    print('\n'.join(reversed(moves)))


if __name__ == "__main__":
    main()