https://www.acmicpc.net/problem/2468
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
static int num;
static int[][] heights;
boolean[][] visited;
public void dfs(int x, int y, int h) {
int[] dx = {-1, 0, 1, 0};
int[] dy = {0, -1, 0, 1};
visited[x][y] = true;
for(int i = 0; i < 4; i++) {
int cx = x + dx[i];
int cy = y + dy[i];
if(cx >= 0 && cx < num && cy >= 0 && cy < num) {
if(!visited[cx][cy] && heights[cx][cy] > h) {
dfs(cx, cy, h);
}
}
}
}
public int getMaxNumOfSafety(int max) {
int result = 0;
for(int i = 0; i <= max; i++) {
visited = new boolean[num][num];
int count = 0;
for(int j = 0; j < heights.length; j++) {
for(int l = 0; l < heights[j].length; l++) {
if(!visited[j][l] && heights[j][l] > i) {
count++;
dfs(j, l, i);
}
}
}
result = Math.max(result, count);
}
return result;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
num = Integer.parseInt(br.readLine());
heights = new int[num][num];
int max = 0;
for(int i = 0; i < num; i++) {
String[] input = br.readLine().split(" ");
for(int j = 0; j < num; j++) {
heights[i][j] = Integer.parseInt(input[j]);
max = Math.max(max, heights[i][j]);
}
}
br.close();
Main m = new Main();
bw.write(m.getMaxNumOfSafety(max) + "\n");
bw.flush();
bw.close();
}
}