fork(1) download
  1. import java.util.Scanner;
  2.  
  3.  
  4. public class Main {
  5. static boolean[][] region ;
  6. static int[][] mojaz={{1,0},{0,1},{-1,0},{0,-1}};
  7. static int max=0;
  8. static int cnt=0;
  9. static int c;
  10. static int r;
  11. static int x;
  12. static int y;
  13.  
  14. public static void main(String[] args){
  15. Scanner in = new Scanner(System.in);
  16. char[][] ar ;
  17. String line;
  18.  
  19.  
  20.  
  21. while(in.hasNext()){
  22. r= in.nextInt();
  23.  
  24. c= in.nextInt();
  25. ar=new char[r][c];
  26. region=new boolean[r+2][c+2];
  27. cnt=0;
  28. max=0;
  29.  
  30. for(int i=0;i<r;i++){
  31. line=in.next();
  32. for(int j=0;j<c;j++){
  33. ar[i][j]=line.charAt(j);
  34. }
  35. }
  36.  
  37. x = in.nextInt();
  38. y = in.nextInt();
  39.  
  40. char p = ar[x][y];
  41.  
  42. for(int i=0;i<r;i++){
  43. for(int j=0;j<c;j++){
  44. if(ar[i][j]==p){
  45. region[i][j]=true;
  46. }else
  47. region[i][j]=false;
  48. }
  49. }
  50.  
  51. dfs(x, y);
  52. cnt = 0;
  53. for(int i=0;i<r;i++){
  54.  
  55. for(int j=0;j<c;j++){
  56. if(region[i][j]){
  57. dfs(i,j);
  58. if(cnt>max){
  59. max=cnt;
  60. }
  61. cnt=0;
  62. }
  63. }
  64. }
  65.  
  66.  
  67.  
  68. System.out.println(max);
  69. //in.nextLine();
  70.  
  71. }
  72.  
  73. }
  74.  
  75.  
  76. public static void dfs(int row,int col){
  77. if(region[row][col]){
  78. region[row][col]=false;
  79. cnt++;
  80. for(int i=0;i<mojaz.length;i++){
  81. int dx=mojaz[i][0]+row;
  82. int dy =(c+mojaz[i][1]+col)%c;
  83. if(dx>=0 && dx<r && region[dx][dy]){
  84. dfs(dx, dy);
  85. }
  86. }
  87.  
  88.  
  89.  
  90.  
  91. }
  92.  
  93. }
  94. }
  95.  
Success #stdin #stdout 0.11s 380608KB
stdin
5 5
wwwww
wwllw
wwwww
wllww
wwwww
1 3

6 6
----l-
l---l-
l-----
------
--l---
------
4 2

6 6
l---ll
------
------
------
--l---
------
0 0

20 20
vqqqqqqqqqqqqqqqqqqq
qqqqqqqqqqqqqqqqqqqq
qqqqqqqqqqqqqqqqqqqq
qqqqqqqqqqqqqqqqqqqq
qqqqqqqqqqqqqqqqqqqq
qqqqqqqqqqqqqqqqqqqq
qqqqqqqqqqqqqqqqqqqq
qqqqqqqqqqqqqqqqqqqq
qqqqqqqqqqqqqqqqqqqq
qqqqqqqqVqqqVqqqqqqq
qqqqqqqqqVVVqqqqqqqq
qqqqqqqqqVVVqqqqqqqq
qqqqqqqqqVvVqqqqqqqq
qqqqqqqqVqqqvqqqqqqq
qqqqqqqVqqvqqvqqqqqq
qqqqqqqqqqqqqqqqqqqq
qqqqqqqqqqvqqqqqqqqq
qqqqqqqqqqvqqqqqqqqq
qqqqqqqqqqqqqqqqqqqq
qqqqqqqqqqqqqqqqqqqq
0 0
stdout
2
2
1
2