fm.cpp
#include <vector>
#include <list>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <queue>
#define vi vector <int>
#define vl vector <long long>
#define vpii vector <pair <int,int> >
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define FOR(i,n) for(ll i=0;i<int(n);i++)
#define READ(v,n) {FOR(i,n){ll x;cin>>x;v.pb(x);} }
#define gmin(a,b) {if (b<a) a=b;}
#define gmax(a,b) {if (b>a) a=b;}
#define pb push_back
#define ppb pop_back
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int getxy(long* arr, int x, int y) {
if (x < 0 || y< 0) return 0;
return arr[x*1500 + y];
}
long a[1500*1500];
long b[1500*1500];
int main(){
int ar,ac,tr,tc;
while (cin>>ar>>ac>>tr>>tc) {
for (int i=0; i < 1500*1500; ++i){
a[i] = 0; b[i] = 0;
}
string s;
for (int i=0; i<ar+tr; ++i) {
if (i >= ar) {
for (int j=0; j<ac+tc; ++j) {
a[1500*i + j] = getxy(a, i-1, j) + getxy(a, i, j-1) - getxy(a, (i-1), j-1);
}
} else {
cin >> s;
for (int j=0; j<ac+tc; ++j) {
if (j >= ac) {
a[1500*i + j] = getxy(a, i-1, j) + getxy(a, i, j-1) - getxy(a, (i-1), j-1);
} else
if (s[j] == '.') {
a[1500*i + j] = getxy(a, i-1, j) + getxy(a, i, j-1) - getxy(a, (i-1), j-1);
} else {
a[1500*i + j] = getxy(a, i-1, j) + getxy(a, i, j-1) - getxy(a, (i-1), j-1) + 1;
}
}
}
}
for (int i=0; i<ar+tr; ++i) {
for (int j=0; j<ac+tc; ++j) {
b[1500*i + j] = getxy(b, i-tr, j) + getxy(b, i, j-tc) - getxy(b, (i-tr), j-tc);
if ((getxy(a, i, j) - getxy(a, i-tr, j) - getxy(a, i, j-tc) + getxy(a, (i-tr), j-tc)) > 0) ++b[1500*i + j];
//cout << b[1500 * i + j] << " ";
}
//cout << endl;
}
long m = getxy(b, ar, ac);
for (int i = 0; i < tr; ++i) {
for (int j = 0; j < tc; ++j) {
//if (i <= ar && j <= ac)
if ((getxy(b, ar+i, ac+j) < m)) m = (getxy(b, ar+i, ac+j));
}
}
cout << m<<endl;
}
return 0;
}