public static void Main()
int n = int.Parse(Console.ReadLine());
char[][] matrix = new char[n][];
for (int i = 0; i < matrix.Length; i++)
char[] inputRow = Console.ReadLine().ToCharArray();
int currentKnightsInDanger = 0;
int maxKnightsInDanger = -1;
int mostDangerousKnightRow = 0;
int mostDangerousKnightCol = 0;
for (int rowIndex = 0; rowIndex < matrix.Length; rowIndex++)
for (int colIndex = 0; colIndex < matrix[rowIndex].Length; colIndex++)
if (matrix[rowIndex][colIndex].Equals('K'))
if (IsCellInMatrix(rowIndex - 2, colIndex - 1, matrix))
if (matrix[rowIndex - 2][colIndex - 1].Equals('K'))
currentKnightsInDanger++;
if (IsCellInMatrix(rowIndex - 2, colIndex + 1, matrix))
if (matrix[rowIndex - 2][colIndex + 1].Equals('K'))
currentKnightsInDanger++;
if (IsCellInMatrix(rowIndex + 2, colIndex - 1, matrix))
if (matrix[rowIndex + 2][colIndex - 1].Equals('K'))
currentKnightsInDanger++;
if (IsCellInMatrix(rowIndex + 2, colIndex + 1, matrix))
if (matrix[rowIndex + 2][colIndex + 1].Equals('K'))
currentKnightsInDanger++;
if (IsCellInMatrix(rowIndex - 1, colIndex - 2, matrix))
if (matrix[rowIndex - 1][colIndex - 2].Equals('K'))
currentKnightsInDanger++;
if (IsCellInMatrix(rowIndex - 1, colIndex + 2, matrix))
if (matrix[rowIndex - 1][colIndex + 2].Equals('K'))
currentKnightsInDanger++;
if (IsCellInMatrix(rowIndex + 1, colIndex - 2, matrix))
if (matrix[rowIndex + 1][colIndex - 2].Equals('K'))
currentKnightsInDanger++;
if (IsCellInMatrix(rowIndex + 1, colIndex + 2, matrix))
if (matrix[rowIndex + 1][colIndex + 2].Equals('K'))
currentKnightsInDanger++;
if (currentKnightsInDanger > maxKnightsInDanger)
maxKnightsInDanger = currentKnightsInDanger;
mostDangerousKnightRow = rowIndex;
mostDangerousKnightCol = colIndex;
currentKnightsInDanger = 0;
if (maxKnightsInDanger != 0)
matrix[mostDangerousKnightRow][mostDangerousKnightCol] = 'O';
Console.WriteLine(count);
public static bool IsCellInMatrix(int row, int col, char[][] matrix)
if (0 <= row && row < matrix.Length && 0 <= col && col < matrix[row].Length)