COCI '09 Contest 7 #5 Kraljevi

View as PDF

Submit solution


Points: 15 (partial)
Time limit: 0.6s
Memory limit: 64M

Problem type

Mirko and Slavko are playing a chess-like game. The game is played on a non-standard chessboard sized R rows by C columns. Each player starts with some number of chess kings. In chess, kings can move from their current field to any of the 8 neighbouring fields.

Player spread is defined as the complete sum of distances between all pairs of pieces of the given player. The distance between two pieces is the smallest number of moves required for both pieces to reach the same field. No actual moves are performed when calculating the distance and as such enemy pieces do not influence the result.

Mirko knows that the spread is a vital piece of strategic information and would like you to make him a program that will calculate both his and Slavko's spread.

Input Specification

The first line of input contains two integers R and C (1 \le R, C \le 1\,000), the number of rows and columns.

The next R lines contain C characters each. Character M denotes Mirko's piece, S denotes Slavko's piece and . denotes an empty field.

There is at least one piece per player on the board. Otherwise the game would be over.

Output Specification

In the first and only line of output, you need to print exactly two integers. The first integer is the spread of Mirko's and the second Slavko's pieces.

Scoring

In test cases worth 20\% of total points, the number of pieces on the board will be less than or equal to 5\,000.

In test cases worth 60\% of total points, R and C will be smaller than or equal to 300.

Sample Input 1

2 3
SMS
MMS

Sample Output 1

3 5

Sample Input 2

2 3
S.M
M..

Sample Output 2

2 0

Sample Input 3

4 5
M....
..S.M
SS..S
.M...

Sample Output 3

10 13

Comments

There are no comments at the moment.