import java.io.*; import java.util.*;
public class Main { public static int maxValue = 0; public static int[] dy = { 1, -1, 0, 0 }; public static int[] dx = { 0, 0, 1, -1 };
public static void findVirus(int N, int M, int[][] lab) { int count = 0;
for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (lab[i][j] == 0) { count++; } } }
if (count > maxValue) { maxValue = count; } }
public static void bfs(int N, int M, int[][] lab) { Queue<int[]> q = new LinkedList(); boolean[][] check = new boolean[N][M];
for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (lab[i][j] == 2) { check[i][j] = true; q.add(new int[] { i, j }); } } }
while (!q.isEmpty()) { int[] front = q.remove(); int cntY = front[0]; int cntX = front[1];
for (int i = 0; i < 4; i++) { int ny = cntY + dy[i]; int nx = cntX + dx[i];
if (0 > ny || ny >= N || 0 > nx || nx >= M) { continue; }
if (lab[ny][nx] == 0 && check[ny][nx] == false) { lab[ny][nx] = 2; check[ny][nx] = true; q.add(new int[] { ny, nx }); } } } }
public static void copyLab(int[][] copiedLab, int[][] lab, int N, int M) { for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { copiedLab[i][j] = lab[i][j]; } } }
public static void makeWall(int N, int M, int[][] lab, int depth) { if (depth == 3) { int[][] copiedLab = new int[N][M];
copyLab(copiedLab, lab, N, M); bfs(N, M, lab); findVirus(N, M, lab); copyLab(lab, copiedLab, N, M);
return; }
for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (lab[i][j] != 0) { continue; } lab[i][j] = 1; makeWall(N, M, lab, depth + 1); lab[i][j] = 0; } } }
public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] inputs = br.readLine().split(" "); int N, M;
N = Integer.parseInt(inputs[0]); M = Integer.parseInt(inputs[1]);
int[][] lab = new int[N][M];
for (int i = 0; i < N; i++) { inputs = br.readLine().split(" ");
for (int j = 0; j < M; j++) { lab[i][j] = Integer.parseInt(inputs[j]); } }
makeWall(N, M, lab, 0);
System.out.println(maxValue); } }
|