N,M=input().split()
N,M=int(N),int(M)
L=[]
h=''
for i in range(N):
s=input().split()
L+=[s]
Q=int(input())
U=[]
for i in range(Q):
s=input().split()
U+=[s]
for i in U:
a=0
for z in L:
T=True
for j in range(M):
if i[j]!='-1' and i[j]!=z[j]:
T=False
break
if T:
a+=1
print(a)