fm.cpp
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cctype>
#include<climits>
#include<algorithm>
#include<utility>
#include<string>
#include<deque>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
#define REP(i,N) for (int i = 0; i < (N); i++)
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORI(i,a,b) for (int i = (a); i < (b); i++)
#define FORD(i,a,b) for (int i = (a)-1; i >= (b); i--)
#define DP(arg...) fprintf(stderr, ## arg)
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
int W, H;
int w, h;
int S[2222][2222];
int res[1111][1111];
char mapa[2222][2222];
int oblast(int x, int y) {
int ans = S[x][y] + S[x-w][y-h];
ans -= S[x][y-h] + S[x-w][y];
return ans;
}
void solve() {
REP(i, W+4*w) REP(j, H+4*h) {
S[i][j] = 0;
mapa[i][j] = '.';
}
REP(i, w+5) REP(j, h+5) res[i][j] = 0;
REP(i, W) REP(j, H) {
char c; scanf(" %c", &c);
mapa[i+w][j+h] = c;
}
FOR (i, 1, W+2*w) {
int pref = 0;
FOR (j, 1, H+2*h) {
pref += (mapa[i][j]=='X')?1:0;
S[i][j] = pref + S[i-1][j];
}
}
FOR(i, w, W+2*w) FOR(j, h, H+2*h) {
if (oblast(i, j)>0) res[i%w][j%h]++;
}
int ans = INT_MAX;
REP(i, w) REP(j, h) ans = min(ans, res[i][j]);
printf("%d\n", ans);
}
int main() {
while (scanf("%d%d%d%d", &W, &H, &w, &h) != EOF) {
solve();
}
return 0;
}