Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- public class Main {
- public static int rows;
- public static int cols;
- public static ArrayList<String>grid;
- public static boolean [][]visited;
- public static int cnt;
- public static boolean isValid(int r,int c)
- {
- if((r>=0 && r<rows) && (c>=0 && c<cols))
- return true;
- else
- return false;
- }
- public static void floodFill(int r,int c)
- {
- if(!isValid(r,c) || grid.get(r).charAt(c)=='0' || visited[r][c])
- return;
- visited[r][c]=true;
- cnt++;
- floodFill(r+1, c); // Down
- floodFill(r-1, c); // UP
- floodFill(r, c-1); // Left
- floodFill(r, c+1); // Right
- floodFill(r-1, c-1); // UP-Left
- floodFill(r+1, c-1); // Down -Left
- floodFill(r-1, c+1); // UP - Right
- floodFill(r+1, c+1); // Down - Right
- }
- public static void main(String[] args) throws NumberFormatException, IOException {
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- StringBuilder ans = new StringBuilder();
- int t = Integer.parseInt(in.readLine());
- in.readLine();
- for(int i=0;i<t;i++)
- {
- cnt =0;
- grid = new ArrayList<String>();
- String temp;
- while((temp = in.readLine())!=null && temp.length()!=0 )
- grid.add(temp);
- rows = grid.size();
- cols = grid.get(0).length();
- visited = new boolean [rows][cols];
- for(int j=0;j<rows;j++)
- {
- for(int k=0;k<cols;k++)
- {
- int tm = cnt;
- cnt=0;
- if(!visited[j][k] && grid.get(j).charAt(k)!='0')
- floodFill(j, k);
- cnt = Math.max(cnt, tm);
- }
- }
- ans.append(cnt).append("\n");
- if(i!=t-1)
- ans.append("\n");
- }
- System.out.print(ans.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement