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
import numpy as np

n, m = map(int, input().split())

a = np.arange(0)
rows = np.arange(1, n + 1)
cols = np.arange(1, m + 1)

for i in range(n):
    inputRow = input()
    a = np.append(a, list(inputRow.strip()))

a = a.reshape(n, m)

result = ""
commandsCount = 0

rowsLeft = n
colsLeft = m

while rowsLeft > 0 and colsLeft > 0:
    rowsIter = rows.copy()
    for rowI in rowsIter[rowsIter != 0]:
        tmp = np.unique(np.delete(a[rowI - 1], np.argwhere(a[rowI - 1] == '0')))
        if np.count_nonzero(tmp) == 1:
            result = f"R {rowI} {tmp[0]}\n" + result
            commandsCount += 1
            rows[rowI - 1] = 0
            a[rowI - 1] = 0
            rowsLeft -= 1

    colsIter = cols.copy()
    for colI in colsIter[colsIter != 0]:
        tmp = np.unique(np.delete(a[:, colI - 1], np.argwhere(a[:, colI - 1] == '0')))
        if np.count_nonzero(tmp) == 1:
            result = f"K {colI} {tmp[0]}\n" + result
            commandsCount += 1
            cols[colI - 1] = 0
            a[:, colI - 1] = 0
            colsLeft -= 1

print(commandsCount)
print(result.strip())