#include <bits/stdc++.h>
using namespace std;
char fj[105][105];
int gr[105][105];
int n, m;
int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int main()
{
cin >> n;
int ans = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> fj[i][j];
if (fj[i][j] == '@')
{
gr[i][j] = 1;
ans++;
}
}
}
cin >> m;
for (int t = 2; t <= m; t++)
{
int z = t - 1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (gr[i][j] == z)
{
for (int k = 0; k < 4; k++)
{
int x = i + dir[k][0];
int y = j + dir[k][1];
if (x >= 1 && x <= n && y >= 1 && y <= n && fj[x][y] != '#' && gr[x][y] == 0)
{
gr[x][y] = t;
ans++;
}
}
}
}
}
}
cout << ans;
return 0;
}