#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define PRINT(a) printf("%.15Lf\n",a);
#define YESNO(a) cout << (a ? "YES" : "NO") << endl
#define YesNo(a) cout << (a ? "Yes" : "No") << endl
#define yesno(a) cout << (a ? "yes" : "no") << endl
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12 
//priority_queue<int,vector<int>, greater<int> > q2;



signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int H, W; cin >> H >> W;
	VS S(H);
	REP(i, H)cin >> S[i];
	int ans = 0;
	REP(i, H - 1) {
		REP(j, W - 1) {
			int cnt = 0;
			if (S[i][j] == '#')cnt++;
			if (S[i][j + 1] == '#')cnt++;
			if (S[i + 1][j] == '#')cnt++;
			if (S[i + 1][j + 1] == '#')cnt++;
			if (cnt == 1 || cnt == 3)ans++;
		}
	}
	cout << ans << endl;
	return 0;
}


// Author: Pawan Raj
// Problem: E - Filters
// Contest: AtCoder - AtCoder Beginner Contest 196
// URL: https://atcoder.jp/contests/abc196/tasks/abc196_e

#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define int long long
#define F(i, n) for (int i = 0; i < n; i++)
#define all(x)(x).begin(), (x).end()
#define show(A) for (auto i: A) cout << i << " "; cout << '\n';
#define show_(A, n) F(i, n) { show(A[i]) }

using namespace std;
using ld = long double;
using vi = vector < int > ;
using mi = map < int, int > ;
using pi = pair < int, int > ;

const int N = 100005;
const int MOD = 1e9 + 7;
const int inf = 1e18 + 1;
/***************************code begins here*****************************/

void solve() {
    int n; cin >> n;
    
    int H = 1e9, L = -1e9, T = 0;
    F(i, n){
    	int x,y;
    	cin >> x >> y;
    	if(y==1){
    		L += x;
    		H += x;
    		T += x;
    	}
    	else if(y==2){
    		L = max(L,x);
    		H = max(H,x);
    	}
    	else{
    		L = min(L,x);
    		H = min(H,x);
    	}
    }
    int q; cin >> q;
    while(q--){
    	int x; cin >> x;
    	x += T;
    	x = min(x,H);
    	x = max(x,L);
    	cout << x << '\n';
    }
}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    // cin >> t;
    while (t--) solve();

    return 0;
}