// Arup Guha
// 11/17/2024
// Solution to 2024 SER D2 Problem F: Office Building

import java.util.*;

public class officebuilding {

	public static int r;
	public static int c;
	public static int[][] pts;

	public static void main(String[] args) {
	
		// Set up...
		Scanner stdin = new Scanner(System.in);
		r = stdin.nextInt();
		c = stdin.nextInt();
		pts = new int[r][c];
		int total = 0;
		
		// Read grid.
		for (int i=0; i<r; i++) {
			for (int j=0; j<c; j++) {
				pts[i][j] = stdin.nextInt();
				total += pts[i][j];
			}
		}
		
		// Read one mold.
		int gr = stdin.nextInt();
		int gc = stdin.nextInt();
		boolean[][] g1 = new boolean[gr][gc];
		for (int i=0; i<gr; i++) {
			char[] tmp = stdin.next().toCharArray();
			for (int j=0; j<gc; j++)
				g1[i][j] = (tmp[j] == '#');
		}
		
		// Get initial.
		int sub = getScore(g1);
		
		// Try 3 more rotations.
		for (int i=0; i<3; i++) {
			g1= rotate(g1);
			sub = Math.min(sub, getScore(g1));
		}
		
		// Ta da!
		System.out.println(total-sub);
	}
	
	// Returns a 90 degree clockwise rotation of g.
	public static boolean[][] rotate(boolean[][] g) {
	
		// Set these up.
		int myr = g.length;
		int myc = g[0].length;
		
		// Copy answer here.
		boolean[][] res = new boolean[myc][myr];
		
		// Here's the pattern read the columns backwards...
		for (int i=0; i<myc; i++)
			for (int j=0; j<myr; j++)
				res[i][j] = g[myr-1-j][i];
				
		return res;
	}
	
	public static int getScore(boolean[][] g) {
		
		// Will be overwritten.
		int best = 101*r*c;
		
		// (i,j) is top left corner of placing this mold.
		for (int i=0; i<=r-g.length; i++) {
			for (int j=0; j<=c-g[0].length; j++) {
				
				int tmp = 0;
				
				// Loop through pattern.
				for (int a=0; a<g.length; a++)
					for (int b=0; b<g[0].length; b++)
						
						// Add if we axe this tree!
						if (g[a][b])
							tmp += pts[i+a][j+b];
				
				// Update.
				best = Math.min(best, tmp);
			}
		}
		
		// Ta da!
		return best;
	}
}