//https://www.hackerrank.com/challenges/cavity-map
import java.io.*;
import java.util.*;

public class Solution{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        //Input
        byte N = Byte.parseByte(br.readLine());
        char[][] matrix = new char[N][];
        for(byte i = 0; i < N; ++i){
            matrix[i] = br.readLine().toCharArray();
        }
        
        //Solve
        
        //Find cavities
        int max = N-1;
        List<Byte> cavX = new ArrayList<Byte>();
        List<Byte> cavY = new ArrayList<Byte>();
        for(byte y = 1; y < max; ++y){
            for(byte x = 1; x < max; ++x){
                char depth = matrix[y][x];
                if (depth > matrix[y-1][x]
                    && depth > matrix[y+1][x]
                    && depth > matrix[y][x-1]
                    && depth > matrix[y][x+1]
                ){
                    cavY.add(y);
                    cavX.add(x);
                }
            }
        }
        
        //Mark cavities
        int size = cavY.size();
        for(int i = 0; i < size; ++i){
            matrix[cavY.get(i)][cavX.get(i)] = 'X';
        }
        
        //Output
        StringBuffer sb = new StringBuffer();
        for(byte i = 0; i < N; ++i){
            sb.append(new String(matrix[i]));
            sb.append("\n");
        }
        System.out.print(sb);
    }
}

@@ -65,28 +65,8 @@
 o.*;
-%0Aimport java.util.*;
 %0A%0Apu
@@ -84,17 +84,19 @@
 Solution
-%7B
+ %7B%0A
 %0A    pub
@@ -152,16 +152,41 @@
 ption %7B%0A
+        %0A        //INPUT%0A
         
@@ -271,32 +271,13 @@
     
-%0A        //Input%0A       
+final
  byt
@@ -317,24 +317,30 @@
 ));%0A        
+final 
 char%5B%5D%5B%5D mat
@@ -357,16 +357,17 @@
 char%5BN%5D%5B
+N
 %5D;%0A     
@@ -382,47 +382,29 @@
 yte 
-i
+y
  = 0; 
-i
+y
  %3C N;
- ++i)%7B%0A           
  matrix%5B
 i%5D =
@@ -399,17 +399,19 @@
  matrix%5B
-i
+y++
 %5D = br.r
@@ -433,27 +433,58 @@
 rArray()
-;%0A        %7D
+)%7B%7D%0A        br.close();%0A        br = null;
 %0A       
@@ -500,12 +500,12 @@
  //S
-olve
+OLVE
 %0A   
@@ -524,140 +524,160 @@
   //
-Find
+No
  cavit
-ies%0A        int max = N-1;%0A        List%3CByte%3E cavX = new ArrayList%3CByte%3E();%0A        List%3CByte%3E cavY = new ArrayList%3CByte%3E(
+y can be on the edge of%0A        //the matrix, so start at index 1%0A        //and end at index N-1 for y and x%0A        final byte MAX = (byte)(N-1
 );%0A 
@@ -703,19 +703,19 @@
  1; y %3C 
-max
+MAX
 ; ++y)%7B%0A
@@ -750,11 +750,11 @@
 x %3C 
-max
+MAX
 ; ++
@@ -777,18 +777,22 @@
     
-char depth
+final char cur
  = m
@@ -824,21 +824,19 @@
     if (
-depth
+cur
  %3E matri
@@ -840,28 +840,26 @@
 trix%5By-1%5D%5Bx%5D
-%0A  
  
+%0A
             
@@ -858,37 +858,35 @@
              && 
-depth
+cur
  %3E matrix%5By+1%5D%5Bx
@@ -884,17 +884,15 @@
 ix%5By
-+1
 %5D%5Bx
++1
 %5D%0A
-  
     
@@ -900,33 +900,30 @@
             
- 
 && 
-depth
+cur
  %3E matrix%5By%5D
@@ -925,18 +925,15 @@
 ix%5By
++1
 %5D%5Bx
--1%5D%0A   
+%5D%0A
     
@@ -952,13 +952,11 @@
  && 
-depth
+cur
  %3E m
@@ -957,33 +957,33 @@
 ur %3E matrix%5By%5D%5Bx
-+
+-
 1%5D%0A             
@@ -1012,85 +1012,91 @@
     
-cavY.add(y);%0A                    cavX.add(x);%0A                %7D%0A            %7D
+//Take advantage that numbers%0A                    //come before alphabet; 'X' %3E '9'
 %0A   
@@ -1096,34 +1096,32 @@
 '9'%0A        
-%7D%0A
         
 %0A        //M
@@ -1112,155 +1112,63 @@
     
-%0A
     
-    //Mark cavities%0A        int size = cavY.size();%0A        for(int i = 0; i %3C size; ++i)%7B%0A            matrix%5BcavY.get(i)%5D%5BcavX.get(i)%5D = 'X';
+matrix%5By%5D%5Bx%5D = 'X';%0A                %7D%0A            %7D
 %0A   
@@ -1198,13 +1198,13 @@
  //O
-utput
+UTPUT
 %0A   
@@ -1208,16 +1208,22 @@
         
+final 
 StringBu
@@ -1273,25 +1273,21 @@
 yte 
-i
+y
  = 0; 
-i
+y
  %3C N;
- ++i
 )%7B%0A 
@@ -1311,32 +1311,50 @@
 end(
-new String(
 matrix%5B
-i%5D)
+y%5D
 );%0A
+            if (++y %3C N)%7B%0A    
     
@@ -1378,16 +1378,30 @@
 (%22%5Cn%22);%0A
+            %7D%0A
         
@@ -1426,16 +1426,18 @@
 ut.print
+ln
 (sb);%0A  

Update cavity-map.java