using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("3 3 2 1");
WillReturn.Add("1 1");
WillReturn.Add("2 3");
WillReturn.Add("2 2");
//7
}
else if (InputPattern == "Input2") {
WillReturn.Add("4 4 3 3");
WillReturn.Add("1 2");
WillReturn.Add("1 3");
WillReturn.Add("3 4");
WillReturn.Add("2 3");
WillReturn.Add("2 4");
WillReturn.Add("3 2");
//8
}
else if (InputPattern == "Input3") {
WillReturn.Add("5 5 5 1");
WillReturn.Add("1 1");
WillReturn.Add("2 2");
WillReturn.Add("3 3");
WillReturn.Add("4 4");
WillReturn.Add("5 5");
WillReturn.Add("4 2");
//24
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static int mH;
static int mW;
static int mN;
static int mM;
static int UB_X;
static int UB_Y;
// 電球の存在する座標のハッシュ値のSet
static HashSet<int> mLightHashSet = new HashSet<int>();
// ブロックの存在する座標のハッシュ値のSet
static HashSet<int> mBlockHashSet = new HashSet<int>();
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
SplitAct(InputList[0]);
mH = wkArr[0];
mW = wkArr[1];
mN = wkArr[2];
mM = wkArr[3];
UB_X = mW - 1;
UB_Y = mH - 1;
foreach (string EachStr in InputList.Skip(1).Take(mN)) {
SplitAct(EachStr);
int Hash = GetHash(wkArr[1] - 1, wkArr[0] - 1);
mLightHashSet.Add(Hash);
}
foreach (string EachStr in InputList.Skip(1 + mN)) {
SplitAct(EachStr);
int Hash = GetHash(wkArr[1] - 1, wkArr[0] - 1);
mBlockHashSet.Add(Hash);
}
HashSet<int> SolveResult1 = Solve(0, -1);
HashSet<int> SolveResult2 = Solve(0, +1);
HashSet<int> SolveResult3 = Solve(-1, 0);
HashSet<int> SolveResult4 = Solve(+1, 0);
HashSet<int> Answer = new HashSet<int>(SolveResult1);
Answer.UnionWith(SolveResult2);
Answer.UnionWith(SolveResult3);
Answer.UnionWith(SolveResult4);
Console.WriteLine(Answer.Count);
}
// 照らせる方向のベクトルを引数として、照らした座標のHashSetを返す
static HashSet<int> Solve(int pVectorX, int pVectorY)
{
var WillReturnSet = new HashSet<int>();
foreach (int EachLightHash in mLightHashSet) {
int CurrX, CurrY;
GetXAndYFromHash(out CurrX, out CurrY, EachLightHash);
while (true) {
int CurrHash = GetHash(CurrX, CurrY);
//ブロックの場合
if (mBlockHashSet.Contains(CurrHash)) {
break;
}
//処理済の場合
if (WillReturnSet.Add(CurrHash) == false) {
break;
};
CurrX += pVectorX;
CurrY += pVectorY;
if (CurrX < 0 || UB_X < CurrX) break;
if (CurrY < 0 || UB_Y < CurrY) break;
}
}
return WillReturnSet;
}
// X座標とY座標を引数として、ハッシュ値を返す
static int GetHash(int pX, int pY)
{
return 10000 * pY + pX;
}
// ハッシュ値からX座標とY座標を返す
static void GetXAndYFromHash(out int pX, out int pY, int pHash)
{
pX = pHash % 10000;
pY = pHash / 10000;
}
}