public static void main ( String [ ] args ) {
  Scanner sc = new Scanner ( System . in ) ;
  Queue < Point > que = new ArrayDeque < > ( ) ;
  int dx [ ] = {
    1 , 0 , - 1 , 0 };
    int dy [ ] = {
      0 , 1 , 0 , - 1 };
      int h = sc . nextInt ( ) ;
      int w = sc . nextInt ( ) ;
      int sx = sc . nextInt ( ) - 1 ;
      int sy = sc . nextInt ( ) - 1 ;
      int gx = sc . nextInt ( ) - 1 ;
      int gy = sc . nextInt ( ) - 1 ;
      int dist [ ] [ ] = new int [ h ] [ w ] ;
      for ( int i = 0 ;
      i < h ;
      i ++ ) for ( int j = 0 ;
      j < w ;
      j ++ ) dist [ i ] [ j ] = INF ;
      char f [ ] [ ] = new char [ h ] [ w ] ;
      for ( int i = 0 ;
      i < h ;
      i ++ ) {
        f [ i ] = sc . next ( ) . toCharArray ( ) ;
      }
      dist [ sx ] [ sy ] = 0 ;
      que . add ( new Point ( sx , sy ) ) ;
      while ( ! que . isEmpty ( ) ) {
        Point p = que . poll ( ) ;
        if ( p . x == gx && p . y == gy ) break ;
        for ( int i = 0 ;
        i < 4 ;
        i ++ ) {
          int nx = p . x + dx [ i ] ;
          int ny = p . y + dy [ i ] ;
          if ( f [ nx ] [ ny ] == '.' && dist [ nx ] [ ny ] == INF ) {
            que . add ( new Point ( nx , ny ) ) ;
            dist [ nx ] [ ny ] = dist [ p . x ] [ p . y ] + 1 ;
          }
        }
      }
      System . out . println ( dist [ gx ] [ gy ] ) ;
    }
    
public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; Queue < Point > que = new ArrayDeque < > ( ) ; int dx [ ] = { 1 , 0 , - 1 , 0 } ; int dy [ ] = { 0 , 1 , 0 , - 1 } ; int h = sc . nextInt ( ) ; int w = sc . nextInt ( ) ; int sx = sc . nextInt ( ) - 1 ; int sy = sc . nextInt ( ) - 1 ; int gx = sc . nextInt ( ) - 1 ; int gy = sc . nextInt ( ) - 1 ; int dist [ ] [ ] = new int [ h ] [ w ] ; for ( int i = 0 ; i < h ; i ++ ) for ( int j = 0 ; j < w ; j ++ ) dist [ i ] [ j ] = INF ; char f [ ] [ ] = new char [ h ] [ w ] ; for ( int i = 0 ; i < h ; i ++ ) { f [ i ] = sc . next ( ) . toCharArray ( ) ; } dist [ sx ] [ sy ] = 0 ; que . add ( new Point ( sx , sy ) ) ; while ( ! que . isEmpty ( ) ) { Point p = que . poll ( ) ; if ( p . x == gx && p . y == gy ) break ; for ( int i = 0 ; i < 4 ; i ++ ) { int nx = p . x + dx [ i ] ; int ny = p . y + dy [ i ] ; if ( f [ nx ] [ ny ] == ' . ' && dist [ nx ] [ ny ] == INF ) { que . add ( new Point ( nx , ny ) ) ; dist [ nx ] [ ny ] = dist [ p . x ] [ p . y ] + 1 ; } } } System . out . println ( dist [ gx ] [ gy ] ) ; }
(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: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) arguments: (argument_list (field_access object: (identifier) field: (identifier)))))) (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)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (array_initializer (decimal_integer_literal) (decimal_integer_literal) (unary_expression operand: (decimal_integer_literal)) (decimal_integer_literal)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (array_initializer (decimal_integer_literal) (decimal_integer_literal) (decimal_integer_literal) (unary_expression operand: (decimal_integer_literal))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (identifier)) dimensions: (dimensions_expr (identifier))))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (identifier))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) dimensions: (dimensions) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (identifier)) dimensions: (dimensions_expr (identifier))))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (expression_statement (assignment_expression left: (array_access array: (identifier) index: (identifier)) right: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) name: (identifier) arguments: (argument_list)))))) (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (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 (identifier) (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)))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (identifier)) right: (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (identifier)))) consequence: (break_statement)) (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 (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (array_access array: (identifier) index: (identifier))))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (field_access object: (identifier) field: (identifier)) right: (array_access array: (identifier) index: (identifier))))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (character_literal)) right: (binary_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (identifier)))) consequence: (block (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (object_creation_expression type: (type_identifier) arguments: (argument_list (identifier) (identifier)))))) (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (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: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)))))))