using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Text; using System.Text.RegularExpressions; using System.Linq; public class Program { public static void Main(string[] args) { new Magatro().Solve(); } } class Magatro { private int H, W, K, P; private int[] x, y; private string[] N; private void Scan() { string[] line = Console.ReadLine().Split(' '); H = int.Parse(line[0]); W = int.Parse(line[1]); K = int.Parse(line[2]); P = int.Parse(line[3]); x = new int[K]; y = new int[K]; N = new string[K]; for (int i = 0; i < K; i++) { line = Console.ReadLine().Split(' '); x[i] = int.Parse(line[0]); y[i] = int.Parse(line[1]); N[i] = line[2]; } } public void Solve() { Scan(); int max = -1; int ans = 0; for (int i = 0; i < (1 << K); i++) { if (BitCount(i) != P) { continue; } int cnt = Count(i); if (max < cnt) { ans = i; max = cnt; } } Console.WriteLine(max); for (int i = 0; i < K; i++) { int j = 1 << i; if ((ans & j) != 0) { Console.WriteLine(N[i]); } } } private int Count(int i) { var d = new Dictionary, bool>(); for (int j = 0; j < K; j++) { int k = 1 << j; if ((i & k) == 0) { d[new Pair(x[j], y[j])] = true; } } var dp = new int[H + 1, W + 1]; dp[0, 0] = 1; for (int j = 0; j <= H; j++) { for (int k = 0; k <= W; k++) { bool o; if (d.TryGetValue(new Pair(j,k),out o)) continue; if (j < H) { dp[j + 1, k] += dp[j, k]; } if (k < W) { dp[j, k + 1] += dp[j, k]; } } } return dp[H, W]; } private int BitCount(int n) { n = (n & 0x55555555) + (n >> 1 & 0x55555555); n = (n & 0x33333333) + (n >> 2 & 0x33333333); n = (n & 0x0f0f0f0f) + (n >> 4 & 0x0f0f0f0f); n = (n & 0x00ff00ff) + (n >> 8 & 0x00ff00ff); n = (n & 0x0000ffff) + (n >> 16 & 0x0000ffff); return n; } } struct Pair { public F First; public S Second; public Pair(F f, S s) { First = f; Second = s; } }