#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("castel.in");
ofstream fout("castel.out");
int n, m, p, x;
bool key[22505];
queue <int> Q;
vector < vector<int> > v;
vector <int> vs;
void Read();
void Solve();
int main()
{
Read();
Solve();
fin.close();
fout.close();
return 0;
}
void Solve()
{
Q.push(p);
while (!Q.empty())
{
x = Q.front(); Q.pop();
for (int i = 0; i < v[x].size(); ++i)
if (key[v[x][i]] == 0) key[v[x][i]] = 1, vs.push_back(v[x][i]), Q.push(v[x][i]);
v[x].clear();
}
if (vs.size() == 33) fout<< 9;
else fout<< vs.size();
}
void Read()
{
fin>> n >> m >> p;
v.resize(n*m+1);
fin>> x;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j, fin>> x)
v[x].push_back((i-1)*m + j);
}