Why Did the Cow Cross the Road 6

Given an N x N grid of pastures, some adjacent pairs blocked by roads, and K cows on distinct cells, count the pairs of cows that cannot reach each other without crossing a road.

Medium5GraphBFSUnion-findImplementationInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

The reason the cow crossed the road is simply that there are so many roads. John's farm has so many roads that a cow can hardly move around without crossing one.

John's farm was recently reorganized. It is now an N×NN \times N grid of small square pastures (2N1002 \le N \le 100). A cow can usually walk freely between two adjacent pastures, but some adjacent pairs are separated by a road that the cow must cross to go between them. A tall fence surrounds the farm, so no cow ever leaves it.

There are KK cows on John's farm (1K1001 \le K \le 100, KN2K \le N^2), each in a different pasture. Some pairs of cows cannot meet unless one of them crosses a road. Count how many such pairs there are.

Input

The first line contains NN, KK, and RR.

Each of the next RR lines describes one road. A road joins two pastures that are adjacent vertically or horizontally, and it is given as rr cc rr' cc' (row, column, row, column). Every number is between 11 and NN, inclusive.

Each of the next KK lines gives the position of one cow as a row and a column.

Output

Print the number of pairs of cows that cannot meet without crossing a road.