import java.io.*;
import java.util.*;
public class Main {
static int[] dx = { -1, 0, 1, 0 };
static int[] dy = { 0, 1, 0, -1 };
static int N, M;
static int[][] numbers = new int[3][];
static List<int[]> wall;
static int max = Integer.MIN_VALUE;
static int[][] lab;
static List<int[]> virus = new ArrayList<>();
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
wall = new ArrayList<int[]>();
lab = new int[N][M];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine(), " ");
for (int j = 0; j < M; j++) {
lab[i][j] = Integer.parseInt(st.nextToken());
if (lab[i][j] == 2)
virus.add(new int[] { i, j });
if (lab[i][j] == 0) {
wall.add(new int[] { i, j });
}
}
}
comb(0, 0);
System.out.println(max);
}
static void bfs(int i, int j, int[][] lab_temp, ArrayDeque<int[]> virus) {
boolean[][] v = new boolean[N][M];
while (!virus.isEmpty()) {
int[] cur = virus.poll();
for (int k = 0; k < 4; k++) {
int ni = cur[0] + dx[k];
int nj = cur[1] + dy[k];
if (0 <= ni && ni < N && 0 <= nj && nj < M && !v[ni][nj] && lab_temp[ni][nj] == 0) {
v[ni][nj] = true;
lab_temp[ni][nj] = 2;
virus.add(new int[] { ni, nj });
}
}
}
int count = 0;
for (int x = 0; x < N; x++) {
for (int y = 0; y < M; y++) {
if (lab_temp[x][y] == 0)
count++;
}
}
max = Math.max(max, count);
}
private static void comb(int cnt, int start) {
if (cnt == 3) {
int[][] lab_temp = new int[N][M];
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
lab_temp[i][j] = lab[i][j];
}
}
for (int i = 0; i < 3; i++) {
lab_temp[numbers[i][0]][numbers[i][1]] = 1;
}
ArrayDeque<int[]> vir = new ArrayDeque<>();
for (int i = 0; i < virus.size(); i++) {
vir.offer(virus.get(i));
}
bfs(0, 0, lab_temp, vir);
return;
}
for (int i = start; i < wall.size(); i++) {
numbers[cnt] = wall.get(i);
comb(cnt + 1, i + 1);
}
}
}
- 이차원 배열 복사 시 깊은 복사, 얕은 복사 주의하기