Count the unit squares with all four corners visited by repeating the same N-step walk for K days from each day's endpoint.
Medium7GeometrySimulationMathHash mapNo attempts yetTime limit1sMemory limit256 MBYou live in a city shaped like a grid of roads. Many roads run very far in the north to south direction, and many other roads run very far in the east to west direction. The gap between two neighbouring north to south roads is 1 km. The gap between two neighbouring east to west roads is also 1 km.
The city has one city hall. The crossing where the city hall stands is written as (0,0). Every crossing of this city is written as crossing (i,j) with two integers i and j. Crossing (i,j) is the crossing reached from crossing (0,0) by going i km east (−i km west when i<0) and j km north (−j km south when j<0).
The city hall keeps one dog named Joy. Joy made a walking plan for K days. The plan is the following.
The city hall talks about the territory Joy builds over the K days of walking. If Joy left a mark at least once on all four crossings (a,b), (a+1,b), (a+1,b+1) and (a,b+1), then the block enclosed by these four crossings belongs to Joy's territory.
The roads of this city are very long, and there are enough roads in both the north to south direction and the east to west direction, so Joy never reaches the end of a road or the end of the city during a walk.
Given Joy's walking plan, write a program that computes the number of blocks that belong to Joy's territory.
The first line contains two integers N and K separated by a single space. One day of walking consists of N steps, and the walking plan lasts K days.
The second line contains a string S of length N. The p-th character Cp (1≤p≤N) of S, counted from the left, is one of E, N, W, S. The characters mean the following.
For crossing (i,j), the crossings neighbouring on the east, north, west and south are crossing (i+1,j), crossing (i,j+1), crossing (i−1,j) and crossing (i,j−1).
Print the number of blocks that belong to Joy's territory on one line.