#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
typedef long long LL;
using namespace std;
const int N = 2000010;
int A,B,C,m;
LL s[N],b[N],bp[N]; // s[N]原数组 b[N]差分数组 bp[N]差分数组备份
int d[8][4] = {
{0, 0, 0, 1},
{0, 0, 1, -1},
{0, 1, 0, -1},
{0, 1, 1, 1},
{1, 0, 0, -1},
{1, 0, 1, 1},
{1, 1, 0, 1},
{1, 1, 1, -1}
}; // 二进制顺序记忆 奇数个1为-1
int op[N / 2][7];
int get(int i, int j, int k) // 返回一维坐标
{
return (i * B + j) * C + k;
}
bool check(int mid)
{
memcpy(b, bp, sizeof b);
for (int i = 1; i <= mid; i++)
{
int x1 = op[i][0], x2 = op[i][1], y1 = op[i][2], y2 = op[i][3], z1 = op[i][4], z2 = op[i][5], h = op[i][6];
b[get(x1,y1,z1)] -= h;
b[get(x1,y1,z2 + 1)] += h;
b[get(x1,y2 + 1,z1)] += h;
b[get(x1,y2 + 1,z2 + 1)] -= h;
b[get(x2 + 1,y1,z1)] += h;
b[get(x2 + 1,y1,z2 + 1)] -= h;
b[get(x2 + 1,y2 + 1,z1)] -= h;
b[get(x2 + 1,y2 + 1,z2 + 1)] += h;
}
memset(s, 0, sizeof s); // s数组归零 通过b求前缀和 更新被攻击mid次后的s(战舰生命值)
for (int i = 1; i <= A; i++)
for (int j = 1; j <= B; j++)
for (int k = 1; k <= C; k++)
{
s[get(i,j,k)] = b[get(i,j,k)];
for (int u = 1; u < 8; u++) // u从1开始,执行余下7次操作
{
int x = i - d[u][0], y = j - d[u][1], z = k - d[u][2], c = d[u][3];
s[get(i,j,k)] -= s[get(x,y,z)] * c; // 3维前缀和
}
if (s[get(i,j,k)] < 0) return true; // 已经有战舰炸了 返回true
}
return false;
}
int main()
{
cin >> A >> B >> C >> m;
for (int i = 1; i <= A; i++)
for (int j = 1; j <= B; j++)
for (int k = 1; k <= C; k++)
scanf("%lld", &s[get(i,j,k)]); // 读入初始战舰生命值
for (int i = 1; i <= A; i++)
for (int j = 1; j <= B; j++)
for (int k = 1; k <= C; k++)
for (int u = 0; u < 8; u++)
{
int x = i - d[u][0], y = j - d[u][1], z = k - d[u][2], c = d[u][3];
bp[get(i,j,k)] += s[get(x,y,z)] * c; // 3维差分
}
for (int i = 1; i <= m; i++)
for (int j = 0; j < 7; j++)
scanf("%d",&op[i][j]); // 读入操作
int l = 1, r = m;
while (l < r) // 二分查找
{
int mid = l + r >> 1;
if (check(mid)) r = mid; // check 判断当第mid次攻击发生,是否已经有战舰爆炸
else l = mid + 1;
}
printf("%d", l);
return 0;
}