#include <fstream>
#include <iostream>
#include <string>
#include <cmath>
#include <vector>

using namespace std;

struct rc {
	int r, c;
};

string water[1000];
rc locs[1000];
int waterq, rows, columns, points;
ifstream fin("DATA5.txt");
ofstream fout("OUT5.txt");

void flood() {
	if (water[0][0] == '*' || water[0][0] == '#') return;
	int r = 0, c = 0;

	int down, right;
	while ((down = (r < rows - 1 && (water[r + 1][c] == '.' || water[r + 1][c] == 'A'))) // Can go down
	|| (right = (c < columns - 1) && (water[r][c + 1] == '.' || water[r][c + 1] == 'A'))) { // Can go right
		if (down) {
			r++; continue;
		}
		if (right) {
			c++; continue;
		}
	}
	water[r][c] = '*';
}

void runit() {
	string blackhole;
	fin >> waterq >> columns >> rows;
	points = 0;
	getline(fin, blackhole);
	for (int r = 0; r < rows; r++) {
		getline(fin, water[r]);
		for (int c = 0; c < columns; c++) if (water[r][c] == 'A') {
			locs[points].r = r;
			locs[points].c = c;
			water[r][c] = '.';
			points++;
		}
	}

	getline(fin, blackhole);

	for (int i = 0; i < waterq; i++) flood();

	int flooded = 0;
	for (int i = 0; i < points; i++ ) {
		if (water[locs[i].r][locs[i].c] == '*') flooded++;
	}
	fout << flooded << endl;
}

int main() {
	for (int i = 0; i < 5; i++) runit();
}

