In the year 2008, the 29th Olympic Games will be held in Beijing. This will signify the prosperity of China as well as becoming a festival for people all over the world.
The official mascots of Beijing 2008 Olympic Games are Fuwa, which are named as Beibei, Jingjing, Haunhuan, Yingying and Nini. Fuwa embodies the natural characteristics of the four most popular animals in China -- Fish, Panda, Tibetan Antelope, Swallow -- and the Olympic Flame. To popularize the official mascots of Beijing 2008 Olympic Games, some volunteers make a PC game with Fuwa.
As shown in the picture, the game has a matrix of Fuwa. The player is to find out all the rectangles whose four corners have the same kind of Fuwa. You should make a program to help the player calculate how many such rectangles exist in the Fuwa matrix.
Input
Standard input will contain multiple test cases. The first line of the input is a single integerT (1 <=T <= 50) which is the number of test cases. And it will be followed byT consecutive test cases.
The first line of each test case has two integersM andN (1 <=M,N <= 250), which means the number of rows and columns of the Fuwa matrix. And then there areM lines, each hasN characters, denote the matrix. The characters -- 'B' 'J' 'H' 'Y' 'N' -- each denotes one kind of Fuwa.
Output
Results should be directed to standard output. The output of each test case should be a single integer in one line, which is the number of the rectangles whose four corners have the same kind of Fuwa.
Sample Input
2
2 2
BB
BB
5 6
BJHYNB
BHBYYH
BNBYNN
JNBYNN
BHBYYH
Sample Output
1
8
http://blog.csdn.net/mengxiang000000/article/details/51169341题目大意:给你一个n*m的矩阵,里边有五种福娃,让你在图中找四个角都是一样福娃的矩形个数。
思路:
我们枚举两行,这样如果上边一行和下边一行的相同位子字母相同的话,这样就保证了有一条边符合矩形了。
假如这样的两行:
BBBB
BBBB
一共有四条竖着的边符合条件,辣么能够组成多少个矩形呢?用排列组合的话来说,很简单,就是Cn2.辣么我们这个时候只要枚举三层for即可。