This page is still under construction.

Parts of this page are still being built. What you see may change.

Splitting the Seminar into Two Groups

Time limit1sMemory limit128 MB

Summary
Pick the largest subset of students that splits into two groups with each grade kept together and no disliking pair in the same group.
Level

Medium7 of 10

Topics
Graph, Brute force
Solved
No attempts yet

Problem

Principal Jang Hong-jun of Daegu Science High School is holding a seminar for the tenth anniversary of his appointment. To run it smoothly he splits the students who attend into two groups.

The principal has always treated communication between students as important, so he never puts two students who dislike each other in the same group. On top of that, all attending students of one grade have to go into the same group.

Dislike happens only between two students of different grades. Two students in the same grade never dislike each other.

After many attempts the principal found that keeping both rules while the whole school attends is impossible. He now wants as many students as possible to come while both rules still hold. A student who does not attend joins neither group, so the two rules say nothing about that student.

Write a program that finds the largest number of students that can attend the seminar.

Input

The first line contains the number of students NN. (1≤N≤501 \le N \le 50)

The second line contains the grade of each student with no spaces. The ii-th character is the grade GiG_i of student ii. (1≤Gi≤31 \le G_i \le 3)

Each of the next NN lines contains the relations with no spaces. The jj-th character on line i+2i+2 is 'Y' when student ii and student jj dislike each other, and 'N' otherwise. The table is symmetric about its diagonal, and the diagonal positions and the positions for two students of the same grade are always 'N'.

Output

Print the largest number of students that can attend the seminar.

Examples1

  1. Example 1

    Input
    3
    123
    NYY
    YNY
    YYN
    
    Expected output
    2