https://www.acmicpc.net/problem/1743
14분 컷 - vis true 빼먹어서 좀 더 걸림
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int n,m,k, cnt;
static int[][] trash;
static boolean[][] vis;
static int[] dx = {0,1,0,-1};
static int[] dy = {1,0,-1,0};
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
trash = new int[n+1][m+1]; // 0번 index dummy
vis = new boolean[n+1][m+1];
for(int i=0;i<k;i++){
st = new StringTokenizer(br.readLine());
int r = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
trash[r][c] = 1;
}
int max = Integer.MIN_VALUE;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(trash[i][j] == 1 && !vis[i][j]){
cnt = 1;
dfs(i, j);
if(max<cnt){
max=cnt;
}
}
}
}
System.out.println(max);
}
static void dfs(int x, int y){
vis[x][y] = true;
for(int d=0;d<4;d++){
int nx = x+dx[d];
int ny = y+dy[d];
if(nx<0 || nx>n || ny<0 || ny>m || vis[nx][ny] || trash[nx][ny] ==0) continue;
vis[nx][ny] = true;
dfs(nx,ny);
cnt++;
}
}
}