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 squareshaped 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 costeffectiveness 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
hide comments
minhthai:
20160121 17:10:39
nice problem! disjoint set :) 

gaurav117:
20160118 05:13:12
Be careful with the input. End of line may be '\n' or '\r'. Many useless spaces after m. 

rocode0001:
20151215 10:51:59
Check the flood fill algorithm...thanks chirag for testcases!! 

vibhorg97:
20151023 19:23:48
thanks chirag for the test cases!!


dev:
20151016 07:11:43
MJA AA GYA !!! 

bholagabbar:
20150918 12:07:05
Excellent problem. Thanks for the testcases Chirag!. Hint: DSU ;) 

SangKuan:
20150813 02:35:21
my common lisp code got nezc,but it pass all random test...my c code ac 

shuks:
20150717 23:14:38
Nice one :) 

TP:
20150629 06:55:12
Use cin to input strings , scanf cost me two WA. in C++. 

Prakhar Gupta:
20140813 09:32:27
excellent question 
Added by:  JaceTheMindSculptor 
Date:  20090407 
Time limit:  0.902s1s 
Source limit:  50000B 
Memory limit:  1536MB 
Cluster:  Cube (Intel G860) 
Languages:  All except: C99 ERL JSRHINO 
Resource:  Canadian Computing Competition 2008 Stage 2 Day 2 Problem D 