const
  maxfl = 5000;
  maxn = 50;

var
  ans, tans, i, j, ii, jj, k, a, b, n, m : integer;
  flat : array[1..maxn, 1..maxn] of integer;
  light, need : array[1..maxn, 1..maxn] of boolean;
  agree : array[1..maxfl] of boolean;
  s : string;
  used : array[1..maxfl] of boolean;
begin
  reset(input, 'crazy.in');
  rewrite(output, 'crazy.out');
  readln(n, m);
  assert((1 <= n) and (n <= maxn));
  assert((1 <= m) and (m <= maxn));
  for i := 1 to maxfl do begin
    used[i] := false;
  end;
  for i := 1 to n do begin
    for j := 1 to m do begin
      read(flat[i][j]);
      if (used[flat[i][j]]) then begin
        assert((j <> 1) and (flat[i][j - 1] = flat[i][j]));
      end;
      used[flat[i][j]] := true;
      assert((1 <= flat[i][j]) and (flat[i][j] <= maxfl));
    end;
    readln;
  end;
  for i := 1 to n do begin
    readln(s);
    assert(length(s) = m);
    for j := 1 to m do begin
      if (s[j] = '#') then begin
        light[i][j] := true;
      end else begin
      	assert(s[j] = '.');
        light[i][j] := false;
      end;
    end;
  end;
  readln(a, b);
  assert((1 <= a) and (a <= n));
  assert((1 <= b) and (b <= m));
  for i := 1 to a do begin
    readln(s);
    assert(length(s) = b);
    for j := 1 to b do begin
      if (s[j] = '#') then begin
        need[i][j] := true;
      end else begin
      	assert(s[j] = '.');
        need[i][j] := false;
      end;
    end;
  end;
  ans := maxfl;
  for i := 1 to n - a + 1 do begin
    for j := 1 to m - b + 1 do begin
      for k := 1 to maxfl do begin
        agree[k] := false;
      end;
      for ii := 1 to a do begin
        for jj := 1 to b do begin
          if (need[ii][jj] <> light[i + ii - 1][j + jj - 1]) then begin
            agree[flat[i + ii - 1][j + jj - 1]] := true;
          end;
        end;
      end;
      tans := 0;
      for k := 1 to maxfl do begin
        if (agree[k]) then begin
          tans := tans + 1;
        end;
      end;
      if (tans < ans) then begin
        ans := tans;
      end;
    end;
  end;
  writeln(ans);
end.