while True:
    M, N = [int(i) for i in input().split()]
    if M == 0 and N == 0:
        exit()
    estoque = list()
    for i in range(M):
        pecas = [int(i) for i in input().split()]
        estoque.append(pecas)
    P = int(input())
    qtd = 0
    for i in range(P):
        i, j = [int(i) for i in input().split()]
        if estoque[i-1][j-1] != 0:
            estoque[i-1][j-1] -= 1
            qtd += 1
    print(qtd)
