public static void main ( String [ ] $ ) {
  int r = scanner . nextInt ( ) , c = scanner . nextInt ( ) ;
  Point start = new Point ( scanner . nextInt ( ) - 1 , scanner . nextInt ( ) - 1 ) ;
  Point goal = new Point ( scanner . nextInt ( ) - 1 , scanner . nextInt ( ) - 1 ) ;
  int [ ] [ ] a = IntStream . range ( 0 , r ) . mapToObj ( j -> scanner . next ( ) . chars ( ) . map ( ch -> ch == '#' ? - 1 : 0 ) . toArray ( ) ) . toArray ( int [ ] [ ] :: new ) ;
  Queue < Point > queue = new ArrayDeque < > ( ) ;
  a [ start . x ] [ start . y ] ++ ;
  queue . add ( start ) ;
  while ( ! queue . isEmpty ( ) ) {
    Point point = queue . poll ( ) ;
    for ( int i = 0 ;
    i < 4 ;
    i ++ ) {
      try {
        int x1 = ( new int [ ] {
          - 1 , 1 , 0 , 0 }
          ) [ i ] , y1 = ( new int [ ] {
            0 , 0 , - 1 , 1 }
            ) [ i ] ;
            if ( a [ point . x + x1 ] [ point . y + y1 ] > a [ point . x ] [ point . y ] + 1 || a [ point . x + x1 ] [ point . y + y1 ] == 0 ) {
              a [ point . x + x1 ] [ point . y + y1 ] = a [ point . x ] [ point . y ] + 1 ;
              queue . add ( new Point ( point . x + x1 , point . y + y1 ) ) ;
            }
          }
          catch ( ArrayIndexOutOfBoundsException e ) {
          }
        }
      }
      System . out . println ( a [ goal . x ] [ goal . y ] - 1 ) ;
    }
    
public static void main ( String [ ] $ ) { int r = scanner . nextInt ( ) , c = scanner . nextInt ( ) ; Point start = new Point ( scanner . nextInt ( ) - 1 , scanner . nextInt ( ) - 1 ) ; Point goal = new Point ( scanner . nextInt ( ) - 1 , scanner . nextInt ( ) - 1 ) ; int [ ] [ ] a = IntStream . range ( 0 , r ) . mapToObj ( j -> scanner . next ( ) . chars ( ) . map ( ch -> ch == ' # ' ? - 1 : 0 ) . toArray ( ) ) . toArray ( int [ ] [ ] :: new ) ; Queue < Point > queue = new ArrayDeque < > ( ) ; a [ start . x ] [ start . y ] ++ ; queue . add ( start ) ; while ( ! queue . isEmpty ( ) ) { Point point = queue . poll ( ) ; for ( int i = 0 ; i < 4 ; i ++ ) { try { int x1 = ( new int [ ] { - 1 , 1 , 0 , 0 } ) [ i ] , y1 = ( new int [ ] { 0 , 0 , - 1 , 1 } ) [ i ] ; if ( a [ point . x + x1 ] [ point . y + y1 ] > a [ point . x ] [ point . y ] + 1 || a [ point . x + x1 ] [ point . y + y1 ] == 0 ) { a [ point . x + x1 ] [ point . y + y1 ] = a [ point . x ] [ point . y ] + 1 ; queue . add ( new Point ( point . x + x1 , point . y + y1 ) ) ; } } catch ( ArrayIndexOutOfBoundsException e ) { } } } System . out . println ( a [ goal . x ] [ goal . y ] - 1 ) ; }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (array_type element: (type_identifier) dimensions: (dimensions)) name: (identifier))) body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) arguments: (argument_list (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)) (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)))))) (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) arguments: (argument_list (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)) (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)))))) (local_variable_declaration type: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (decimal_integer_literal) (identifier))) name: (identifier) arguments: (argument_list (lambda_expression parameters: (identifier) body: (method_invocation object: (method_invocation object: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) name: (identifier) arguments: (argument_list)) name: (identifier) arguments: (argument_list (lambda_expression parameters: (identifier) body: (ternary_expression condition: (binary_expression left: (identifier) right: (character_literal)) consequence: (unary_expression operand: (decimal_integer_literal)) alternative: (decimal_integer_literal))))) name: (identifier) arguments: (argument_list))))) name: (identifier) arguments: (argument_list (method_reference (array_type element: (integral_type) dimensions: (dimensions))))))) (local_variable_declaration type: (generic_type (type_identifier) (type_arguments (type_identifier))) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (generic_type (type_identifier) (type_arguments)) arguments: (argument_list)))) (expression_statement (update_expression (array_access array: (array_access array: (identifier) index: (field_access object: (identifier) field: (identifier))) index: (field_access object: (identifier) field: (identifier))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier)))) (while_statement condition: (parenthesized_expression (unary_expression operand: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (decimal_integer_literal)) update: (update_expression (identifier)) body: (block (try_statement body: (block (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (array_access array: (parenthesized_expression (array_creation_expression type: (integral_type) dimensions: (dimensions) value: (array_initializer (unary_expression operand: (decimal_integer_literal)) (decimal_integer_literal) (decimal_integer_literal) (decimal_integer_literal)))) index: (identifier))) declarator: (variable_declarator name: (identifier) value: (array_access array: (parenthesized_expression (array_creation_expression type: (integral_type) dimensions: (dimensions) value: (array_initializer (decimal_integer_literal) (decimal_integer_literal) (unary_expression operand: (decimal_integer_literal)) (decimal_integer_literal)))) index: (identifier)))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (array_access array: (array_access array: (identifier) index: (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (identifier))) index: (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (identifier))) right: (binary_expression left: (array_access array: (array_access array: (identifier) index: (field_access object: (identifier) field: (identifier))) index: (field_access object: (identifier) field: (identifier))) right: (decimal_integer_literal))) right: (binary_expression left: (array_access array: (array_access array: (identifier) index: (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (identifier))) index: (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (identifier))) right: (decimal_integer_literal)))) consequence: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (identifier))) index: (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (identifier))) right: (binary_expression left: (array_access array: (array_access array: (identifier) index: (field_access object: (identifier) field: (identifier))) index: (field_access object: (identifier) field: (identifier))) right: (decimal_integer_literal)))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (object_creation_expression type: (type_identifier) arguments: (argument_list (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (identifier)) (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (identifier)))))))))) (catch_clause (catch_formal_parameter (catch_type (type_identifier)) name: (identifier)) body: (block))))))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (binary_expression left: (array_access array: (array_access array: (identifier) index: (field_access object: (identifier) field: (identifier))) index: (field_access object: (identifier) field: (identifier))) right: (decimal_integer_literal)))))))