/*
ID: divad151
PROG: pname
LANG: C++
*/
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <queue>
#include <cassert>

using namespace std;

//------------------------------------------------------------------------------------------------------------------------
// CONSTANTS
//------------------------------------------------------------------------------------------------------------------------

#define PI          3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803   /* all from memory! */
#define PHI         1.6180339887498948482045868343656   /* = (sqrt(5)+1)/2  */
#define MAXINT      0x7fffffff                          /* for a signed integer */
#define EPSILON     0.00000001                          /* for floating-point value comparison */
#define MAXDOUBLE   1.79769313486231570e+308

#define NORTH       0
#define EAST        1
#define SOUTH       2
#define WEST        3

#define MS(a,b)     memset(a, b, sizeof(a))
#define FOR(a,b,c)  for (a = (b); a < (c); ++a)

const int fact[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600 }; // up to 12!
const int month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};  // lengths of each month


//------------------------------------------------------------------------------------------------------------------------
// GLOBAL VARIABLES
//------------------------------------------------------------------------------------------------------------------------

int dc[] = {  0,  1,  0, -1 };
int dr[] = { -1,  0,  1,  0 };
int cmax = -MAXINT;
int cmin = MAXINT;
int rows, columns;


//------------------------------------------------------------------------------------------------------------------------
// FUNCTIONS/CLASS DEFINITIONS
//------------------------------------------------------------------------------------------------------------------------

bool inBounds(int row, int column)
{
	return (row >= 0 && row < rows && column >= 0 && column < columns);
}





//------------------------------------------------------------------------------------------------------------------------
// MAIN PROGRAM EXECUTION
//------------------------------------------------------------------------------------------------------------------------
int main()
{
    ifstream fin("data5.txt");
    ofstream fout("out5.txt");
    int i = 0, j = 0, k = 0;
    int num, n1, n2, n3;
    string str;

    int nwater;


    for (i =0 ; i <5; ++i)
    {
        string grid[200];
        fin >> nwater >> columns >> rows;
        int counter = 0;
        for (j = 0; j < rows; ++j)
        {
            fin >> grid[j];
        }


        for (j = 0; j < nwater; ++j)
        {

            int r = 0, c = 0;

            if (grid[0][0] == '#') break;

            while (1)
            {
                r++;
                if (!(inBounds(r,c) && grid[r][c] != '#'))
                {
                    --r;
                    c++;
                    if (!(inBounds(r,c) && grid[r][c] != '#'))
                    {
                        c--;
                        break;
                    }
                }
            }

            if (grid[r][c] == 'A') counter++;
            grid[r][c] = '#';

        }

        fout << counter << endl;
    }












    return 0;
}




