Employee Salaries

0

0 votes
Problem

You are working in the HR department of a huge corporation. Each employee may have several direct managers and/or several direct subordinates. Of course, his subordinates may also have their own subordinates, and his direct managers may have their own managers. We say employee X is a boss of employee Y if there exists a sequence of employees A, B, ..., D, such that X is the manager of A, A is the manager of B, and so on, and D is the manager of Y (of course, if X is a direct manager of employee Y, X will be a boss of employee Y). If A is a boss of B, then B can not be a boss of A. According to the new company policy, the salary of an employee with no subordinates is 1. If an employee has any subordinates, then his salary is equal to the sum of the salaries of his direct subordinates.

You will be given a String[] relations, where the j-th character of the i-th element is 'Y' if employee i is a direct manager of employee j, and 'N' otherwise. Find the sum of the salaries of all the employees.

Input:
The first line of the input contains an integer T (1<=T<=10), the number of test cases. The T test cases follow. Each test case contains an integer n (1<=n<=40), followed by n lines, each containing a string of length n.

Output:
For each test case, output consists of single line with the required answer - sum of the salaries of all the employees.

Time Limit: 1
Memory Limit: 256
Source Limit:
Editor Image

?