fork download
  1. #define all(v) v.begin() , v.end()
  2. #define ll long long
  3. #define LL LLONG_MAX
  4. #include <algorithm>
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8. #define cin(a) for(auto &it : a) cin>>it
  9. #define cout(a) for(auto &it : a) cout<<it<<" "
  10. #define endl "\n"
  11. #include <stack>
  12. #include <queue>
  13. #include <set>
  14. #include <map>
  15. #include <limits.h>
  16. #include <cmath>
  17. using namespace std;
  18. const int N = 1e5+5;
  19. #define int ll
  20. int dx[] = {1, 0, -1, 0};
  21. int dy[] = {0, 1, 0, -1};
  22. int oo = LL;
  23. int n,m;
  24. char arr[1005][1005];
  25. bool vis[1005][1005];
  26. void dfs(int i,int j){
  27. vis[i][j]=1;
  28. for (int k = 0; k < 4; k++)
  29. {
  30. if( arr[i+dx[k]][j+dy[k]]=='.'){
  31. if(vis[i+dx[k]][j+dy[k]]==0) dfs(i+dx[k],j+dy[k]);
  32. }
  33. }
  34. }
  35. void solve(){
  36. cin >>n>>m;
  37. for (int i = 0; i < n; i++)
  38. {
  39. for (int j = 0;j < m;j++)
  40. {
  41. cin >>arr[i][j];
  42. }
  43. }
  44. int cnt=0;
  45. for (int i = 0; i < n; i++)
  46. {
  47. for (int j = 0;j < m;j++)
  48. {
  49. if(arr[i][j]=='.' && vis[i][j]==0){
  50. dfs(i,j);
  51. cnt++;
  52. }
  53. }
  54. }
  55. cout << cnt<<endl;
  56. }
  57. signed main()
  58. {
  59. ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
  60. // written by _DeadKiller.
  61. int t=1;
  62. // cin >>t;
  63. while (t--)
  64. {
  65. solve();
  66. }
  67.  
  68.  
  69.  
  70. }
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
0