cswork / OAOpen practice workspace

OpenAIOA #7

Grid Infection Spread Until Stable

Estimated MediumJudge-ready

Topics (AI-labelled)SimulationGrid Search

At the end of each day, a healthy cell . becomes X if it had at least T infected neighbors X at the start of that day. Neighbors include up, down, left, right, and the four diagonals, 8 directions in total. Infected cells never recover. Return the last day on which a new infection occurs; if the grid is already stable initially, return 0. A check day with no change is not counted.

Input

The first line contains n m, the second line contains T, followed by n lines of m consecutive characters. 1≤n,m≤200, 0≤T≤8. Each character is X or .

Output

A single non-negative integer, the number of rounds in which new infections occur.

Example 1

Input:

3 3
1
...
.X.
...

Output:

1

Example 2

Input:

3 3
2
...
.X.
...

Output:

0

Example 3

Input:

1 4
0
....

Output:

1

Example notes

In Example 1 the center infection spreads to all non-immune neighboring cells at once, so the answer is 1. In Example 2 there is only 1 infected neighbor while the threshold is 2, so the answer is 0. In Example 3 the threshold is 0, so no infection source is needed, and every healthy cell is infected on day 1.

Editorial

Loading solution…