def extract_arguments(fh):
    datasets = []
    while True:
        H, W = map(int, fh.readline().strip().split())
        if H == 0 and W == 0:
            break
        grid = [fh.readline().strip() for _ in range(H)]
        n = int(fh.readline().strip())
        chars = fh.readline().strip().split()
        datasets.append((H, W, grid, n, chars))
    return datasets

# Example usage:
# with open('input.txt', 'r') as fh:
#     inputs = extract_arguments(fh)
#     for dataset in inputs:
#         print(dataset)