fork download
  1. const maxr = 100;
  2. var r,c,i,j:byte;
  3. dem: word;
  4. a: array[0..maxr,0..maxr+1] of char;
  5. visit: array[0..maxr+1,0..maxr+1] of boolean;
  6. procedure dfs(i,j:longint);
  7. begin
  8. visit[i,j]:=true;
  9. if (i>1) and (a[i-1,j]='#') and (not visit[i-1,j]) then dfs(i-1,j);
  10. if (i<r) and (a[i+1,j]='#') and (not visit[i+1,j]) then dfs(i+1,j);
  11. if (j>1) and (a[i,j-1]='#') and (not visit[i,j-1]) then dfs(i,j-1);
  12. if (j<c) and (a[i,j+1]='#') and (not visit[i,j+1]) then dfs(i,j+1);
  13. end;
  14. begin
  15. readln(r,c);
  16. for i:=1 to r do
  17. for j:=1 to c do
  18. visit[i,j]:=false;
  19. for i:=1 to r do
  20. begin
  21. for j:=1 to c do
  22. read(a[i,j]);
  23. readln;
  24. end;
  25. dem:=0;
  26. for i:=1 to r do
  27. for j:=1 to c do
  28. if (a[i,j]='#') and (not visit[i,j]) then
  29. begin
  30. inc(dem);
  31. dfs(i,j);
  32. end;
  33. writeln(dem);
  34. end.
Success #stdin #stdout 0s 300KB
stdin
5 6
.#....
..#...
..#..#
...##.
.#....
stdout
5