import sys
h, w = input().strip().split()
h = int(h)
w = int(w)
img = []

for i in range(h):
    line = sys.stdin.readline().strip()
    line = list(map(int, line.split(' ')))
    img.append(line)

kernel = []
m = int(input())

for i in range(m):
    line = sys.stdin.readline().strip()
    line = list(map(float, line.split(' ')))
    kernel.append(line)
w_ = w - m + 1
h_ = h - m + 1
res = []


def conv(img, kernel, x, y, m):
    t = 0
    for i in range(m):
        for j in range(m):
            t += img[i+x][j+y] * kernel[i][j]
    return int(min(255, t))


for i in range(h_):
    tmp = []
    for j in range(w_):
        tmp.append(conv(img, kernel, i, j, m))
    res.append(tmp)

for i in res:
    i=[str(x) for x in i]
    print(' '.join(i))