local bls, brs = bit.lshift, bit.rshift local bxor = bit.bxor local function badd(a, b) return (a + b) % mod end local w, h, k, p = io.read("*n", "*n", "*n", "*n", "*l") h, w = h + 1, w + 1 local row, col, name = {}, {}, {} for i = 1, k do local a, b, c = io.read():match("(%d+) (%d+) (%w+)") col[i] = tonumber(a) + 1 row[i] = tonumber(b) + 1 name[i] = c end local map = {} for i = 1, h do map[i] = {} for j = 1, w do map[i][j] = true end end for i = 1, k do map[row[i]][col[i]] = false end local function grayCode(x) return bxor(x, brs(x, 1)) end local function grayWalk(size, add_func, rm_func, work_func) local prv = 0 local total = bls(1, size) - 1 local bpos = {} for i = 1, size do bpos[bls(1, i - 1)] = i end work_func() for i = 1, total do local v = grayCode(i) if prv < v then prv, v = v, v - prv add_func(bpos[v]) else prv, v = v, prv - v rm_func(bpos[v]) end work_func() end end local box = 0 local bcnt = 0 local t = {} for i = 1, h do t[i] = {} end local retmax, retbox = 0LL, 0 local function add_func(idx) map[row[idx]][col[idx]] = true bcnt = bcnt + 1 box = box + bls(1, idx - 1) end local function rm_func(idx) map[row[idx]][col[idx]] = false bcnt = bcnt - 1 box = box - bls(1, idx - 1) end local function work_func() if bcnt == p then t[1][1] = 1LL for j = 2, w do if map[1][j] then if t[1][j - 1] < 0LL then t[1][j] = -1LL else t[1][j] = 1LL end else t[1][j] = -1LL end end for i = 2, h do if map[i][1] then t[i][1] = t[i - 1][1] else t[i][1] = -1LL end for j = 2, w do if map[i][j] then if t[i][j - 1] < 0LL and t[i - 1][j] < 0LL then t[i][j] = -1LL elseif t[i][j - 1] < 0LL then t[i][j] = t[i - 1][j] elseif t[i - 1][j] < 0LL then t[i][j] = t[i][j - 1] else t[i][j] = t[i - 1][j] + t[i][j - 1] end else t[i][j] = -1LL end end end if retmax < t[h][w] then retmax, retbox = t[h][w], box end end end grayWalk(k, add_func, rm_func, work_func) if retmax == 0LL then print(0) else retmax = tostring(retmax % 1000000007LL):gsub("LL", "") print(retmax) local i = 0 while 0 < retbox do i = i + 1 if retbox % 2 == 1 then print(name[i]) end retbox = brs(retbox, 1) end end