HERDING - Herding

Oh no! A number of stray cats have been let loose in the city, and as the City Cat Catcher, you have been assigned the vital task of retrieving all of the cats. This is an ideal opportunity to test your latest invention, a cat trap which is guaranteed to retrieve every cat which walks into a square-shaped subsection of the city.

Fortunately, you have the assistance of one of the world's foremost cat psychologists, who has the amazing ability of predicting, given a square subsection of the city, exactly which of the four cardinal directions (north, east, south or west) the cat will head. While this information is handy, you still don't know where all the cats currently are.

In order to prove the cost-effectiveness of your method to the City it would, of course, be important to minimize the number of traps used.

Input

The input will begin with a line consisting of two numbers n and m, separated by a space (1 ≤ n, m ≤ 1000). The city will be an n x m grid of square subsections. The next n lines will each consist of a string of length m, consisting of the letters 'N', 'E', 'S', or 'W', representing north, east, south and west, respectively. (The first character of the first line will be the northwesternmost point.) The direction in the square is the direction which cats will head if they are in that square. The cat psychologist assures you that cats have no interest in leaving the city.

Output

Output the minimum number of traps needed.

Example

Input:
3 4
SWWW
SEWN
EEEN

Output:
2

Added by:JaceTheMindSculptor
Date:2009-04-07
Time limit:0.902s-1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: C99 ERL JS-RHINO
Resource:Canadian Computing Competition 2008 Stage 2 Day 2 Problem D

hide comments
2012-03-11 22:32:20 Nic Roets
Be aware of extra blanks after 'm'.

@Mahesh You are testing for N and S along the sides, which is meaningless.
2013-06-24 11:32:39 Shaurya Gupta
Why it is characterised as a BFS(Breadth First Search) problem. It does not needs BFS to be solved!!
2011-05-19 14:28:03 Mahesh Chandra Sharma
REP(i, n) if (s[i][0]=='N' || s[i][m-1]=='S') assert(1);
REP(i, m) if (s[0][i]=='E' || s[m-1][i]=='W') assert(1);

This is the simple checker which I used and it gives SIGABRT. This means input is faulty. Am I missing something?

Last edit: 2011-05-19 14:28:14
2011-05-18 22:05:21 LeppyR64
@Mahesh - the cats don't wantto leave. The input fits the condition. If ou still question, post your code to the forum.

Last edit: 2011-05-18 22:06:19
2011-05-16 21:59:21 Mahesh Chandra Sharma
problem says: The cat psychologist assures you that cats have no interest in leaving the city.
But I have checked that input doesn't satisfy this condition.
2011-03-20 14:50:22 sudipto das
what is the output for:
3 4
SWWW
SWEN
EEEN

2?
2010-10-14 10:50:33 :D
Please go to the forum->ProblemSet Archive and start a subject there with your code included. We can't just GUESS the problem.
2010-10-14 07:02:14 Gaurav Munjal
I am getting Runtime error NZEC in java. cana anyone help...
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.