eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків

Gamers

As you probably know, the streets and crossroads of Bucharest form a perfect grid. What you probably don't know, is that there is exactly one gaming club and exactly one gamer on each of these crossroads. Strangely enough, each gaming club offers exactly one game. Gamers are also a bit strange, but they generally live a simple life, determined by the following rules: \begin{enumerate} \item A gamer never plays in the club of his own crossroads. Never!!! \item During a day each gamer has to play each of the games when this is not contradicting the Rule \textbf{1}. \item Gamers travel from one crossroads to another only by horizontals or verticals. \item A gamer can not go directly from one club to another. It has to come back home and eat something first and has to finish the day in his own intersection. \item A gamer has to live optimally, thus always picking the best possible strategy to implement the above rules, so that she has enough time for gaming. \end{enumerate} Having in mind that all the gamers are the same, the Association of Computer Maniacs decided that the city should be optimized in order to minimize the total non-gaming effort. The non-gaming effort is the number of crossroads that a gamer walks trough the day. The total non-gaming effort is the sum of the non-gaming efforts for all gamers in the city. In order to perform the optimizations, ACM needs a program that calculates the total non-gaming effort for a given description of the city. \InputFile Several city descriptions are given in the standard input. Each of them starts with a line of two integers \textbf{R} and \textbf{C} (\textbf{1} ≤ \textbf{R}, \textbf{C} ≤ \textbf{1000}), denoting the number of rows and the number of columns of the crossroads in the city. \textbf{R} lines of \textbf{C} characters follow, denoting the kind of game that is offered by the club of each of the crossroads. Each game is coded using a single digit ('\textbf{0}' to '\textbf{9}'). \OutputFile For each of the city descriptions the program has to write on a separate line of the standard output a line containing one integer -- the total non-gaming effort for the city.
Ліміт часу 4 секунди
Ліміт використання пам'яті 64 MiB
Вхідні дані #1
2 2
12
12
3 3
122
313
321
Вихідні дані #1
16
72

Пояснення: In first case, each of the four gamers has to play games 1 and 2. Hopefully, both are only one intersection away, so the non-gaming effort for each gamer is 4 (from home to club 1, from club 1 to home, from home to club 2 and from club 2 to home). That me