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

public class Solution {

    public static void main(String[] args) throws IOException {
        
        //INPUT
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        final byte N = Byte.parseByte(br.readLine());
        final char[][] matrix = new char[N][N];
        for(byte y = 0; y < N; matrix[y++] = br.readLine().toCharArray()){}
        br.close();
        br = null;
        
        //SOLVE
        
        //No cavity can be on the edge of
        //the matrix, so start at index 1
        //and end at index N-1 for y and x
        final byte MAX = (byte)(N-1);
        for(byte y = 1; y < MAX; ++y){
            for(byte x = 1; x < MAX; ++x){
                final char cur = matrix[y][x];
                if (cur > matrix[y-1][x] 
                 && cur > matrix[y][x+1]
                 && cur > matrix[y+1][x]
                 && cur > matrix[y][x-1]
                ){
                    //Take advantage that numbers
                    //come before alphabet; 'X' > '9'
                    matrix[y][x] = 'X';
                }
            }
        }
        
        //OUTPUT
        final StringBuffer sb = new StringBuffer();
        for(byte y = 0; y < N;){
            sb.append(matrix[y]);
            if (++y < N){
                sb.append("\n");
            }
        }
        System.out.print(sb);
    }
}

@@ -1,55 +1,4 @@
-//https://www.hackerrank.com/challenges/cavity-map%0A
 impo
@@ -753,16 +753,67 @@
 %5By%5D%5Bx%5D;%0A
+                %0A                //Check if cavity%0A
         
@@ -842,15 +842,24 @@
 ix%5By
--1
 %5D%5Bx
-%5D 
++1%5D)%7B //Right
 %0A   
@@ -864,35 +864,39 @@
                 
-&& 
+   if (
 cur %3E matrix%5By%5D%5B
@@ -897,15 +897,25 @@
 ix%5By
+-1
 %5D%5Bx
-+1%5D%0A
+%5D //Top%0A    
     
@@ -948,15 +948,26 @@
 ix%5By
-+1
 %5D%5Bx
-%5D%0A
+-1%5D //Left%0A    
     
@@ -1000,15 +1000,25 @@
 ix%5By
++1
 %5D%5Bx
--1%5D%0A
+%5D //Bottom%0A 
     
@@ -1029,19 +1029,93 @@
         
+   
 )%7B%0A
+                        %0A                        //Mark as cavity.%0A    
         
@@ -1156,16 +1156,20 @@
 numbers%0A
+    
         
@@ -1234,16 +1234,20 @@
         
+    
 matrix%5By
@@ -1258,16 +1258,184 @@
  = 'X';%0A
+                    %7D%0A                    %0A                    //Skip next index since it %0A                    //can't be cavity (cur %3E right)%0A                    ++x;%0A
         

Update cavity-map.java