#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <cassert>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <list>
#include <iomanip>
#include <fstream>
#include <bitset>

#ifdef LOCAL
#include "local.h"
#endif

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(a) (a).begin(), (a).end()
#define clr(a, x) memset(a, x, sizeof(a))
#define sz(a) ((int)(a).size())
#define mp(a, b) make_pair(a, b)
#define ten(n) ((long long)(1e##n))

template <typename T, typename U> void upmin(T& a, const U& b) { a = min<T>(a, b); }
template <typename T, typename U> void upmax(T& a, const U& b) { a = max<T>(a, b); }
template <typename T> void uniq(T& a) { sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); }
template <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); }
template <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; }
void fast_io() { cin.tie(0); ios::sync_with_stdio(false); }
bool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; }

typedef long long ll;
typedef pair<int, int> pint;

const int dx[] = { 0, 1, 0, -1 };
const int dy[] = { 1, 0, -1, 0 };


int main()
{
    int h, w;
    cin >> h >> w;
    int a[64][64], b[64][64];
    rep(y, h) rep(x, w)
        cin >> a[y][x];
    rep(y, h) rep(x, w)
        cin >> b[y][x];

    int ch, cw;
    int c[64][64];
    cin >> ch >> cw;
    rep(y, ch) rep(x, cw)
        cin >> c[y][x];

    const int inf = ten(9);
    int res = -inf;
    rep(sy, h - ch + 1) rep(sx, w - cw + 1)
    {
        int s = 0;
        bool ok = true;
        rep(y, ch) rep(x, cw)
        {
            ok &= c[y][x] == b[sy + y][sx + x];
            s += a[sy + y][sx + x];
        }
        if (ok)
            upmax(res, s);
    }
    if (res == -inf)
        cout << "NA" << endl;
    else
        cout << res << endl;
}
