#include<iostream>
using namespace std;
int n,m;
const int maxn=100+5;
char s[maxn][maxn];
int dir[8][2]=
{
1,1,
-1,1,
1,0,
-1,0,
1,-1,
-1,-1,
0,-1,
0,1
};
void dfs(int x,int y)
{
s[x][y]='*';
for(int i=0;i<8;i++)
{
int tx=x+dir[i][0],ty=y+dir[i][1];
if(tx<0||tx>=n||ty<0||ty>=m)continue;
if(s[tx][ty]=='@')dfs(tx,ty);
}
return ;
}
int main()
{
while(cin>>n>>m&&m)
{
int ans=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin>>s[i][j];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(s[i][j]=='@')
{
dfs(i,j);
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int maxn=100000+10;
char s[maxn/1000][maxn/1000];
int n,m;
int derict[4][2]=
{
-1,0,
1,0,
0,1,
0,-1,
};
int ans;
bool flag[maxn/1000][maxn];
void dfs(int x,int y)
{
for(int i=0;i<4;i++)
{
int xx=x+derict[i][0];
int yy=y+derict[i][1];
if(xx>=0 && xx<n && yy>=0 && yy<m && flag[xx][yy] && s[xx][yy] == '.')
{
ans++;
flag[xx][yy] = false;
dfs(xx,yy);
}
}
return ;
}
int main()
{
while(~scanf("%d%d",&m,&n) && (n || m))
{
memset(flag , true ,sizeof(flag));
for(int i=0;i<n ;i++)
scanf("%s",s[i]);
ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(s[i][j] == '@')
{
flag[i][j] = false;
ans++;
dfs(i,j);
break;
}
}
}
cout<<ans<<endl;
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
const int maxn=100000+10;
char s[maxn/1000][maxn/1000];
int n,m;
int derict[4][2]=
{
-1,0,
1,0,
0,1,
0,-1,
};
int ans;
void dfs(int x,int y)
{
for(int i=0;i<4;i++)
{
int xx=x+derict[i][0];
int yy=y+derict[i][1];
if(xx>=0 && xx<n && yy>=0 && yy<m && s[xx][yy] == '.')
{
ans++;
s[xx][yy ] = '*';
dfs(xx,yy);
}
}
return ;
}
int main()
{
while(~scanf("%d%d",&m,&n) && (n || m))
{
for(int i=0;i<n ;i++)
scanf("%s",s[i]);
ans=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(s[i][j] == '@')
{
s[i][j]= '*';
ans++;
dfs(i,j);
break;
}
}
}
cout<<ans<<endl;
}
return 0;
}
Lake Counting poj2386//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=100000+10;
char s[maxn/1000][maxn/1000];
int n,m;
int derict[8][2]=
{
-1,0,
1,0,
0,1,
0,-1,
1,1,
1,-1,
-1,1,
-1,-1,
};
void dfs(int x,int y)
{
for(int i=0;i<8;i++)
{
int xx=x+derict[i][0];
int yy=y+derict[i][1];
if(xx>=0 && xx < n&& yy>=0 && yy<m && s[xx][yy] == 'W')
{
s[xx][yy] = '.';
dfs(xx,yy);
}
}
return ;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
int ans=0;
for(int i =0;i<n;i++)
scanf("%s",s[i]);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(s[i][j] == 'W')
{
s[i][j]= '.';
dfs(i,j);
ans++;
}
}
}
cout<<ans<<endl;
}
return 0;
}
#include <bits/stdc++.h>
//#include<iostream>
//#include<cstdio>
using namespace std;
const int maxn=100000+10;
char s[maxn/1000][maxn/1000];
int temp;
char tempc;
int n,m;
int derict[4][2]=
{
-1,0,
1,0,
0,-1,
0,1,
};
bool flag[maxn/1000][maxn/1000];
void dfs(int x,int y)
{
for(int i=0;i<4;i++)
{
int xx=x+derict[i][0];
int yy=y+derict[i][1];
if(xx>=0 && xx<n && yy>=0 && yy < m && flag[xx][yy] && s[xx][yy ] == tempc)
{
flag[xx][yy] = false;
temp++;
dfs(xx,yy);
}
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
int ans=0;
memset(flag,true ,sizeof(flag));
for(int i=0;i<n;i++)
scanf("%s",s[i]);
for(int i=0 ;i <n;i++)
{
for(int j=0;j<m;j++)
{
if(flag[i][j])
{
temp=1;
tempc=s[i][j];
flag[i][j] = false;
dfs(i,j);
// cout<<i+1<<" "<<j+1<<" "<<temp<<" "<<tempc<<endl;
if(temp > ans)
ans = temp;
}
}
}
cout<<ans<<endl;
}
return 0;
}
#include<iostream>
using namespace std;
const int maxn=100+5;
char map[maxn][maxn];
int dir[4][2]=
{
1,0,-1,0,0,-1,0,1
};
int n,m;
int ans;
void dfs(int x,int y,int k)
{
map[x][y]=0;
for(int i=0;i<4;i++)
{
int dx=x+dir[i][0];
int dy=y+dir[i][1];
if(dx>=0&&dx<n&&dy>=0&&dy<m&&map[dx][dy]==k)
{
ans++;
dfs(dx,dy,k);
//cout<<ans<<endl;
}
}
}
int main()
{
while(cin>>n>>m)
{
for(int i=0;i<n;i++)
cin>>map[i];
int maxm=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
ans=0;
if(map[i][j])
{
ans++;dfs(i,j,map[i][j]);}
//cout<<ans<<endl;
if(ans>maxm)
maxm=ans;
}
cout<<maxm<<endl;
}
return 0;
}