h1, w1 = gets.chomp.split.map(&:to_i)
arr1, arr2, arr3, cum = [], [], [], Array.new(h1){[0]}
h1.times{arr1 << gets.chomp.split.map(&:to_i)}
h1.times{arr2 << gets.chomp.split.map(&:to_i)}
h3, w3 = gets.chomp.split.map(&:to_i)
h3.times{arr3 << gets.chomp.split.map(&:to_i)}
arr1.each_with_index{|ar, i| ar.each_with_index{|x, j| cum[i][j+1] = cum[i][j] + x}}

max = nil
for y in 0..(h1-h3)
    for x in 0..(w1-w3)
        same = true
        for addy in 0..(h3-1)
            if arr2[y + addy][x, w3] != arr3[addy][0, w3]
                same = false
                break
            end
        end
        if same
            sum = 0
            for addy in 0..(h3-1)
                sum += cum[y + addy][x + w3] - cum[y + addy][x]
            end
            max = sum if max.nil? || max < sum
        end
    end
end
puts max.nil? ? "NA" : max

