eolymp
bolt
Try our new interface for solving problems
Problems

Replication

Replication

The ill-fated result of watching too many "do it yourself" engineering videos on the web, Farmer John has accidentally released a self-replicating robot on his farm!

The farm can be represented by an n × n grid where each grid cell is either empty or filled with rock, and all border squares are filled with rock. Some non-rock cells are designated as possible starting locations for the robot.

Farmer John initially places the robot at one of the possible starting positions. In every hour that follows, all copies of the robot move in one coordinated mass in the same direction, either north, south, east, or west. After every d hours, every copy of the robot replicates - a robot at cell (x, y) that replicates creates new copies in cells (x + 1, y), (x1 , y), (x, y + 1) and (x, y1); the original robot remains at (x, y). Over time, multiple robots might come to occupy the same cell.

If moving or replicating would cause any of the robots to move into a rock, then all robots shut down immediately. Note that this implies that the robots must eventually shut down, due to the border of the farm being rock.

Help the cows figure out the number of empty squares that could potentially at some point in time hold a robot.

Input

The first line contains two integers n (3n1000) and d (1d109). The next n lines of input each contain n characters. Each character is one of '.', 'S' or '#'. '.' and 'S' both represent empty cells, with 'S' denoting a possible starting position for the robot. '#' denotes a rock.

All characters in the first and last row and first and last column are '#'.

Output

Print an integer counting the number of cells that could at some point in time hold a robot.

Example 1

In the following diagrams, x's denote robots. Locations that could be occupied by robots:

##########
#xxx.....#
#xxxx....#
#xxx.....#
##########
#xx..xxx.#
##########
##########
##########
##########

One possible sequence of events could be as follows:

  • FJ places the robot at the upper-left-most starting position.
  • The robot moves one unit to the right.
  • The robot replicates.
  • All robots move one unit to the right.
  • Another replication would cause a copy of the robot to move into a rock, so the process terminates.
##########    ##########    ##########    ##########
#........#    #........#    #.x......#    #..x.....#
#x.......#    #.x......#    #xxx.....#    #.xxx....#
#........#    #........#    #.x......#    #..x.....#
########## -> ########## -> ########## -> ##########
#........#    #........#    #........#    #........#
##########    ##########    ##########    ##########
##########    ##########    ##########    ##########
##########    ##########    ##########    ##########
##########    ##########    ##########    ##########
Time limit 1 second
Memory limit 128 MiB
Input example #1
10 1
##########
#........#
#S.......#
#........#
##########
#S....S..#
##########
##########
##########
##########
Output example #1
15
Input example #2
10 2
##########
#.#......#
#.#......#
#S.......#
#.#......#
#.#......#
##########
##########
##########
##########
Output example #2
28
Source 2020 USACO December, Gold