Do you know Just Odd Inventions Co., Ltd.? The business of this company is doing "just odd inventions." Here we just call it JOI Company.
JOI Company developed new virus "JOI Virus." JOI Company wants to do an experiment by infecting the inhabitants of IOI Island with JOI Virus.
IOI Island takes a rectangular shape. There are
In IOI Island, there are
Every inhabitant has a status "resistance." The resistance of inhabitant
- If
equals , it means that inhabitant has high resistance and he or she doesn't get infected with JOI Virus. - If
is a positive integer, it means that inhabitant may get infected with JOI Virus. If the following condition continues for time periods, he or she will get infected from the next time period:- The inhabitant living in the adjacent section on the direction which wind is blowing from is already infected with JOI Virus.
Note that the last time period of a day and the first time period of the next day is continuous.
With respect to the experiment's purpose, we want to infect at least
Given the direction wind blow from in each time period and the resistance of each inhabitant, write a program which calculates the minimum number of infected inhabitants after
Input Specification
Read the following data from the standard input.
N
, S
, W
or E
. The N
stands for North, S
stands for South, W
stands for West and E
stands for East.
Output Specification
Write the two lines to the standard output.
The first line should contain the minimum number of the infected inhabitants after
Constraints
. . . is a string with length , only containsN
,S
,W
, andE
. .- There is at least
pair such that .
Subtasks
- (14 points)
only containsW
andE
. - (6 points)
, . - (80 points) There are no additional constraints.
Sample Input 1
6 3 4
SWNEES
2 1 1 2
1 0 1 3
1 1 2 2
Sample Output 1
8
8
Explanation for Sample 1
Let us consider the condition that we choose inhabitant
- For inhabitant
, during the time period of the day , wind blow from South and the adjacent inhabitant on South is already infected, so he or she will get infected from the time period of the day . - For inhabitant
, during the time period of the day , wind blow from West and the adjacent inhabitant on West is already infected, so he or she will get infected from the time period of the day . - For inhabitant
, during the time period of the day , wind blow from South and the adjacent inhabitant on South is already infected, and during the time period of the day , wind blow from South and the adjacent inhabitant on South is already infected, so he or she will get infected from the time period of the day . - For inhabitant
, during the time period of the day , wind blow from West and the adjacent inhabitant on West is already infected, so he or she will get infected from the time period of the day . - For inhabitant
, during the time period of the day , wind blow from West and the adjacent inhabitant on West is already infected, so he or she will get infected from the time period of the day . - For inhabitant
, during the time period of the day , wind blow from North and the adjacent inhabitant on North is already infected, so he or she will get infected from the time period of the day . - For inhabitant
, during the time period of the day , wind blow from West and the adjacent inhabitant on West is already infected, and during the time period of the day , wind blow from North and the adjacent inhabitant on North is already infected, so he or she will get infected from the time period of the day .
No more inhabitant will be infected with JOI Virus. Hence, when we choose inhabitant
Whichever inhabitant we choose as the first infected person, We can't make the number of inhabitants infected with JOI Virus after
Sample Input 2
4 4 4
EWWE
1 2 1 2
1 1 1 1
0 0 0 0
2 2 2 4
Sample Output 2
3
3
This sample input and output satisfies the constraints of Subtask 1.
Comments