#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef vector VI; typedef pair PII; #define REP(i,s,t) for(int i=(s);i<(t);i++) #define FILL(x,v) memset(x,v,sizeof(x)) const int INF = (int)1E9; #define MAXN 25 void rotateClockwise(int &n, int &m, char g[][MAXN], char ng[][MAXN], bool writeBack=false) { REP(i,0,m) REP(j,0,n) ng[i][j] = g[n-1-j][i]; if (writeBack) { swap(n, m); REP(i,0,n) REP(j,0,m) g[i][j] = ng[i][j]; } } int g[MAXN][MAXN]; char b[MAXN][MAXN], nb[MAXN][MAXN]; int main() { int r, c, s, t, sum = 0; cin >> r >> c; REP(i,0,r) REP(j,0,c) { cin >> g[i][j]; sum += g[i][j]; } cin >> s >> t; REP(i,0,s) cin >> b[i]; int ans = INF; int ansi, ansj, ansr; REP(it,0,4) { REP(i,0,r) REP(j,0,c) { if (i + s > r || j + t > c) continue; int sol = 0; REP(ii,0,s) REP(jj,0,t) { if (b[ii][jj] == '#') sol += g[i + ii][j + jj]; } if (sol < ans) { ansi = i; ansj = j; ansr = it; } ans = min(ans, sol); } rotateClockwise(s, t, b, nb, true); } cerr << ansi << " " << ansj << " " << ansr << endl; cout << sum - ans << endl; return 0; }