#include <iostream>
#include <string>
#include <fstream>
#include <cmath>
#include <algorithm>

using namespace std;

int water, cols, rows;
string map[10];
string omap[10];

const int RIGHT = 1;
const int DOWN = 2;

void pourwater() {
	if (map[0][0] == '#') return;

	int curr = 0;
	int curc = 0;

	int flow;

	do {
		flow = 0;
		if (curr != rows - 1 && (map[curr + 1][curc] == '.' || map[curr + 1][curc] == 'A')) {
			flow = DOWN;
			curr++;
		} else if (curc != cols - 1 && (map[curr][curc + 1] == '.' || map[curr][curc + 1] == 'A')) {
			flow = RIGHT;
			curc++;
		}
	} while (flow != 0);

	map[curr][curc] = '*';
}

int numocc;

int main() {
	ifstream in("DATA5.txt");
	ofstream out("OUT5.txt");

	for (int cc = 0; cc < 5; cc++) {
		numocc = 0;

		in >> water >> cols >> rows;

		for (int i = 0; i < rows; i++) {
			in >> map[i];
			omap[i] = map[i];
		}

		for (int i = 0; i < water; i++) {
			pourwater();
		}

		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				if (omap[i][j] == 'A' && map[i][j] == '*') {
					numocc++;
				}
			}
		}

		out << numocc << endl;
	}
}

