President of Berland has a very vast office-room, where, apart from him, work his subordinates. Each subordinate, as well as President himself, has his own desk of a unique colour. Each desk is rectangular, and its sides are parallel to the office walls. One day President decided to establish an assembly, of which all his deputies will be members. Unfortunately, he does not remember the exact amount of his deputies, but he remembers that the desk of each his deputy is adjacent to his own desk, that is to say, the two desks (President's and each deputy's) have a common side of a positive length.
The office-room plan can be viewed as a matrix with n rows and m columns. Each cell of this matrix is either empty, or contains a part of a desk. An uppercase Latin letter stands for each desk colour. The «period» character («.») stands for an empty cell.
The first line contains two separated by a space integer numbers n, m (1 ≤ n, m ≤ 100) — the length and the width of the office-room, and c character — the President's desk colour. The following n lines contain m characters each — the office-room description. It is guaranteed that the colour of each desk is unique, and each desk represents a continuous subrectangle of the given matrix. All colours are marked by uppercase Latin letters.
Print the only number — the amount of President's deputies.
3 4 R G.B. .RR. TTT.
2
3 3 Z ... .H. ..Z
0
一个办公室里有好多矩形桌子
不同桌子不同颜色
给出总统的桌子的颜色
总统的副手的桌子跟总统的桌子相邻
问有多少个副手
找到总统的桌子
然后顺着桌子边缘找一圈即可
#include <iostream>
#include <stdio.h>
#include <map>
using namespace std;
const int M=105;
char ti[M][M];
map<char,int>ma;
int main()
{
int n,m;
char ss[M],pr;
int l,r,u,d; //president的桌子占得位置
scanf("%d%d%s",&n,&m,ss);
pr=ss[0];
for(int i=1;i<=n;i++)
scanf("%s",ti[i]+1);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if(ti[i][j]==pr)
{
l=j,u=i;
for(;;i++)
if(ti[i][j]!=pr)
{
i--;
//cout<<" 1111";
break;
}
d=i;
for(;;j++)
if(ti[i][j]!=pr)
{
j--;
//cout<<" 1111";
break;
}
r=j;
}
}
//cout<<l<<r<<u<<d;
if(u>1)
for(int i=l;i<=r;i++)
if('A'<=ti[u-1][i]&&ti[u-1][i]<='Z')
ma[ti[u-1][i]]++;
if(d<n)
for(int i=l;i<=r;i++)
if('A'<=ti[d+1][i]&&ti[d+1][i]<='Z')
ma[ti[d+1][i]]++;
if(l>1)
for(int i=u;i<=d;i++)
if('A'<=ti[i][l-1]&&ti[i][l-1]<='Z')
ma[ti[i][l-1]]++;
if(r<m)
for(int i=u;i<=d;i++)
if('A'<=ti[i][r+1]&&ti[i][r+1]<='Z')
ma[ti[i][r+1]]++;
printf("%d\n",ma.size());
return 0;
}