A version with reduced amount of iterations, fixed off-by-one error, more simple and answering the question about max/min finding.
// (x,y) is the center point;
// assert ( x>=0 && y>=0 && x<n && y<n );
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE, m = n-1;
for( int i = (x==0)? 0 : x-1, x_max = (x==m)? m : x+1; i <= x_max; i++ )
for( int j = (y==0)? 0 : y-1, y_max = (y==m)? m : y+1; j <= y_max; j++ ) {
if( arr[i][j] < min )
min = arr[i][j];
else if ( arr[i][j] > max )
max = arr[i][j];
}
// now you have the max/min values set
or, if you prefer a more verbose version without ternary operator (slower, but more readable for beginners):
// (x,y) is the center point
int x_min = x - 1, y_min = y - 1,
x_max = x + 1, y_max = y + 1,
min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
if ( x_min < 0 )
x_min = 0;
if ( y_min < 0 )
y_min = 0;
if ( x_max > n - 1 ) // you can set 'int m = n - 1;'
x_max = n - 1; // and use m here instead if you wish
if ( y_max > n - 1 )
y_max = n - 1;
for( int i = x_min; i <= x_max; i++ )
for( int j = y_min; j <= y_max; j++ ) {
if( arr[i][j] < min )
min = arr[i][j];
else if ( arr[i][j] > max )
max = arr[i][j];
}
// now you have the max/min values set