object Main extends App {
  import scala.io.StdIn._

  val Array(heights, width) = readLine.trim.split(' ').map(_.toInt)
  val scores = Array.tabulate(heights){_ ⇒ readLine.trim.split(' ').map(_.toInt)}.let{a ⇒
    val array = Array.tabulate(heights + 1){_ ⇒ Array.tabulate(width + 1){_ ⇒ 0}}
    for (y ← a.indices; x ← a(y).indices){
      array(y + 1)(x + 1) = a(y)(x)
      array(y + 1)(x + 1) += array(y)(x + 1) + array(y + 1)(x) - array(y)(x)
    }
    array
  }
  val target = new Target(Array.tabulate(heights){_ ⇒ readLine.trim.split(' ').map(_.toInt == 0)})
  val Array(h, w) = readLine.trim.split(' ').map(_.toInt)
  val pattern = new Pattern(Array.tabulate(h){_ ⇒ readLine.trim.split(' ').map(_.toInt == 0)})
  val suspects = (0 to width - w).flatMap(x ⇒ (0 to heights - h).map(y ⇒ Coordinate(x, y))).filter{o ⇒
      pattern.indices.forall(c ⇒ target(o + c) == pattern(c))
    }.map{o ⇒
      scores(o.y + h)(o.x + w) - scores(o.y)(o.x + w) - scores(o.y + h)(o.x) + scores(o.y)(o.x)
    }
  println(
    if (suspects.isEmpty) "NA"
    else suspects.max
  )
  class Pattern(private val state: Array[Array[Boolean]]) {
    val width = state.head.length
    val height = state.length
    val indices = (0 until width).flatMap(x ⇒ (0 until height).map(y ⇒ Coordinate(x, y)))
    def isValidIndex(coordinate: Coordinate) = (0 until width).contains(coordinate.x) && (0 until height).contains(coordinate.y)
    def apply(coordinate: Coordinate): Boolean = state(coordinate.y)(coordinate.x)
  }
  class Target(private val state: Array[Array[Boolean]]) {
    val width = state.head.length
    val height = state.length
    def isValidIndex(coordinate: Coordinate) = (0 until width).contains(coordinate.x) && (0 until height).contains(coordinate.y)
    def apply(coordinate: Coordinate): Boolean = state(coordinate.y)(coordinate.x)
  }
  case class Coordinate(x: Int, y: Int) {
    def +(that: Coordinate) = Coordinate(this.x + that.x, this.y + that.y)
  }
  implicit class Scope[T](val value: T) extends AnyVal{
    @inline
    def also(@inline func: T ⇒ Unit): T = {
      func(value)
      value
    }
    @inline
    def let[A](func: T ⇒ A): A = func(value)
  }
}
