using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Text; using System.Text.RegularExpressions; using System.Linq; using System.IO; class Program { static void Main(string[] args) { new Magatro().Solve(); } } public class Scanner { private StreamReader Sr; private string[] S; private int Index; private const char Separator = ' '; public Scanner(Stream source) { Index = 0; S = new string[0]; Sr = new StreamReader(source); } private string[] Line() { return Sr.ReadLine().Split(Separator); } public string Next() { string result; if (Index >= S.Length) { S = Line(); Index = 0; } result = S[Index]; Index++; return result; } public int NextInt() { return int.Parse(Next()); } public double NextDouble() { return double.Parse(Next()); } public long NextLong() { return long.Parse(Next()); } public decimal NextDecimal() { return decimal.Parse(Next()); } public string[] StringArray() { Next(); Index = S.Length; return S.ToArray(); } public int[] IntArray() { return StringArray().Select(int.Parse).ToArray(); } public long[] LongArray() { return StringArray().Select(long.Parse).ToArray(); } public bool EndOfStream { get { return Sr.EndOfStream; } } } class Magatro { int N; public void Solve() { var cin = new Scanner(Console.OpenStandardInput()); int h = cin.NextInt(); int w = cin.NextInt(); int k = cin.NextInt(); int p = cin.NextInt(); int[] x = new int[k]; int[] y = new int[k]; string[] n = new string[k]; int mod = (int)1e9 + 7; for (int i = 0; i < k; i++) { x[i] = cin.NextInt(); y[i] = cin.NextInt(); n[i] = cin.Next(); } bool[] ans = null; int max = 0; for (int i = 0; i <= 1 << k; i++) { bool[] b = new bool[k]; int cnt = 0; int cpi = i; for(int j = 0; j < k; j++) { if(cpi%2 == 1) { cnt++; b[j] = true; } cpi /= 2; } if (cnt != p) { continue; } int[,] tr = new int[h + 1, w + 1]; for(int j = 0; j < k; j++) { if (!b[j]) { tr[x[j], y[j]] = -1; } } tr[0, 0] = 1; for(int j = 0; j <= h; j++) { for(int l = 0; l <= w; l++) { if (tr[j, l] == -1) continue; if (j + 1 <= h) { if (tr[j + 1, l] != -1) { tr[j + 1, l] += tr[j, l]; tr[j + 1, l] %= mod; } } if (l + 1 <= w) { if (tr[j, l+1] != -1) { tr[j, l+1] += tr[j, l]; tr[j, l+1] %= mod; } } } } if(tr[h,w] <= 0) { continue; } if (max < tr[h, w]) { ans = b; max = tr[h, w]; } } Console.WriteLine(max); if(max != 0) { for(int i = 0; i < k; i++) { if (ans[i]) { Console.WriteLine(n[i]); } } } } }