#include<bits/stdc++.h> #define SQR(x) ((x)*(x) using namespace std; const int maxn = 1000001; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-')f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x*f; } const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; const int N = 101, M = 101; int n, m, l, r, p, limit, cnt, Time, mice_cnt, cur = -1, mp[N][M], mark[N][M]; struct Mouse { int x, y, dir, part, stun, grow, fuck; bool exist, right, sex; inline void scan() { x = read(); y = read(); char tmp[2]; scanf("%s", tmp); switch (tmp[0]) { case 'N':dir = 0; break; case 'E':dir = 1; break; case 'S':dir = 2; break; case 'W':dir = 3; break; } scanf("%s", tmp); sex = (tmp[0] == 'X'); exist = true; part = -1; } void move() { part = -1; if (mp[x][y] >> dir & 1) { x += dx[dir]; y += dy[dir]; } else { int dr = dir, dl = dir; dr = (dr + 1) % 4; dl = (dl - 1 + 4) % 4; if (!(mp[x][y] >> dl & 1) && !(mp[x][y] >> dr & 1)) { dir = dl; } else { if ((mp[x][y] >> dl & 1) && (mp[x][y] >> dr & 1)) { if (right) dir = dr; else dir = dl; right ^= 1; } else if (mp[x][y] >> dl & 1) { dir = dl; } else { dir = dr; } } } } } mice[10001]; struct Weapon { int type, t, x, y; inline void scan() { type = read(); t = read() + 3 * (type == 3); x = read(); y = read(); } inline void expose() { if (type == 1) { memset(mark, 0, sizeof (mark)); for (int i = 0; i < 4; i++) { int nx = x, ny = y, L = 0; mark[nx][ny] = 1; while (mp[nx][ny] >> i & 1) { nx += dx[i]; ny += dy[i]; if (++L > l)break; mark[nx][ny] = 1; } } for (int i = 1; i <= cnt; i++) if (mark[mice[i].x][mice[i].y]) mice[i].exist = 0; } if (type == 2) { for (int i = 1; i <= cnt; i++) if ((mice[i].x - x)*(mice[i].x - x) + (mice[i].y - y)*(mice[i].y - y) <= r * r) mice[i].stun += 3; } if (type == 3) { for (int i = 1; i <= cnt; i++) if (mice[i].x == x && mice[i].y == y) mice[i].exist = 0; } if (type == 4) { for (int i = 1; i <= cnt; i++) if (mice[i].x == x && mice[i].y == y) mice[i].sex ^= 1; } } } weap[1001]; inline void Move() { for (int i = 1; i <= cnt; i++) if (mice[i].stun)mice[i].stun--; else if (mice[i].exist&&!mice[i].fuck)mice[i].move(); } inline void Grow() { for (int i = 1; i <= cnt; i++) if (!mice[i].stun && mice[i].grow && mice[i].exist) mice[i].grow--; } inline void Fuck() { for (int i = 1; i <= cnt; i++) { if (!mice[i].stun&&!mice[mice[i].part].stun && mice[i].exist && mice[i].fuck && mice[mice[i].part].exist && i < mice[i].part) { mice[i].fuck--; mice[mice[i].part].fuck--; if (!mice[i].fuck) { mice[i].stun = mice[mice[i].part].stun = 1; for (int d = 0; d < 4; ++d) if (mp[mice[i].x][mice[i].y] >> d & 1) { ++cnt; mice[cnt].x = mice[i].x; mice[cnt].y = mice[i].y; mice[cnt].sex = (d % 2 == 0); mice[cnt].grow = 5; mice[cnt].dir = d; mice[cnt].exist = true; mice[cnt].part = -1; } } } } memset(mark, 0, sizeof (mark)); for (int i = 1; i <= cnt; i++) if (mice[i].exist)mark[mice[i].x][mice[i].y]++; for (int i = 1; i < cnt; i++) for (int j = i + 1; j <= cnt; j++) if (!mice[i].fuck && !mice[j].fuck && mice[i].exist && mice[j].exist && !mice[i].grow && !mice[j].grow && mice[i].x == mice[j].x && mice[i].y == mice[j].y && mark[mice[i].x][mice[i].y] == 2 && (mice[i].sex ^ mice[j].sex) == 1 && !mice[i].stun && !mice[j].stun && (mice[i].part != j || mice[j].part != i)) { mice[i].fuck = mice[j].fuck = 2; mice[i].part = j; mice[j].part = i; } } int main() { l = read(); r = read(); n = read(); m = read(); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) mp[i][j] = read(); cnt = read(); for (int i = 1; i <= cnt; i++) mice[i].scan(); p = read(); limit = read(); for (int i = 1; i <= p; i++) weap[i].scan(); Time = read(); while (++cur <= Time) { for (int i = 1; i <= p; i++) if (weap[i].t == cur) weap[i].expose(); Grow(); Fuck(); Move(); mice_cnt = 0; for (int i = 1; i <= cnt; i++) mice_cnt += mice[i].exist; if (mice_cnt > limit) { printf("-1\n"); return 0; } } printf("%d\n", mice_cnt); return 0; }