public static void main ( String [ ] args ) {
  Scanner sc = new Scanner ( in ) ;
  int H = sc . nextInt ( ) ;
  int W = sc . nextInt ( ) ;
  int sy = sc . nextInt ( ) - 1 , sx = sc . nextInt ( ) - 1 , gy = sc . nextInt ( ) - 1 , gx = sc . nextInt ( ) - 1 ;
  int [ ] dx = {
    - 1 , 0 , 1 , 0 };
    int [ ] dy = {
      0 , 1 , 0 , - 1 };
      char [ ] [ ] c = new char [ W ] [ H ] ;
      int [ ] [ ] step = new int [ W ] [ H ] ;
      for ( int i = 0 ;
      i < H ;
      i ++ ) {
        String s = sc . next ( ) ;
        for ( int j = 0 ;
        j < W ;
        j ++ ) {
          c [ j ] [ i ] = s . charAt ( j ) ;
          step [ j ] [ i ] = 100000 ;
        }
      }
      Deque < Point > stack = new ArrayDeque < > ( ) ;
      step [ sx ] [ sy ] = 0 ;
      stack . addFirst ( new Point ( sx , sy ) ) ;
      while ( stack . size ( ) > 0 ) {
        Point p = stack . pollFirst ( ) ;
        for ( int i = 0 ;
        i < 4 ;
        i ++ ) {
          int x = p . x + dx [ i ] ;
          int y = p . y + dy [ i ] ;
          if ( 0 <= x && x < W && 0 <= y && y < H && c [ x ] [ y ] == '.' ) {
            if ( step [ x ] [ y ] > step [ p . x ] [ p . y ] + 1 ) {
              stack . addFirst ( new Point ( x , y ) ) ;
              step [ x ] [ y ] = step [ p . x ] [ p . y ] + 1 ;
            }
          }
        }
      }
      out . println ( step [ gx ] [ gy ] ) ;
    }
    
public static void main ( String [ ] args ) { Scanner sc = new Scanner ( in ) ; int H = sc . nextInt ( ) ; int W = sc . nextInt ( ) ; int sy = sc . nextInt ( ) - 1 , sx = sc . nextInt ( ) - 1 , gy = sc . nextInt ( ) - 1 , gx = sc . nextInt ( ) - 1 ; int [ ] dx = { - 1 , 0 , 1 , 0 } ; int [ ] dy = { 0 , 1 , 0 , - 1 } ; char [ ] [ ] c = new char [ W ] [ H ] ; int [ ] [ ] step = new int [ W ] [ H ] ; for ( int i = 0 ; i < H ; i ++ ) { String s = sc . next ( ) ; for ( int j = 0 ; j < W ; j ++ ) { c [ j ] [ i ] = s . charAt ( j ) ; step [ j ] [ i ] = 100000 ; } } Deque < Point > stack = new ArrayDeque < > ( ) ; step [ sx ] [ sy ] = 0 ; stack . addFirst ( new Point ( sx , sy ) ) ; while ( stack . size ( ) > 0 ) { Point p = stack . pollFirst ( ) ; for ( int i = 0 ; i < 4 ; i ++ ) { int x = p . x + dx [ i ] ; int y = p . y + dy [ i ] ; if ( 0 <= x && x < W && 0 <= y && y < H && c [ x ] [ y ] == ' . ' ) { if ( step [ x ] [ y ] > step [ p . x ] [ p . y ] + 1 ) { stack . addFirst ( new Point ( x , y ) ) ; step [ x ] [ y ] = step [ p . x ] [ p . y ] + 1 ; } } } } out . println ( step [ 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 (identifier))))) (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))) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal))) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal))) 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: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_initializer (unary_expression operand: (decimal_integer_literal)) (decimal_integer_literal) (decimal_integer_literal) (decimal_integer_literal)))) (local_variable_declaration type: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_initializer (decimal_integer_literal) (decimal_integer_literal) (decimal_integer_literal) (unary_expression operand: (decimal_integer_literal))))) (local_variable_declaration type: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) value: (array_creation_expression type: (integral_type) dimensions: (dimensions_expr (identifier)) dimensions: (dimensions_expr (identifier))))) (local_variable_declaration type: (array_type element: (integral_type) dimensions: (dimensions)) declarator: (variable_declarator name: (identifier) 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 (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: (identifier)) update: (update_expression (identifier)) body: (block (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))))) (expression_statement (assignment_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (decimal_integer_literal))))))) (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 (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 (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal))) 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 (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: (binary_expression left: (binary_expression left: (binary_expression left: (decimal_integer_literal) right: (identifier)) right: (binary_expression left: (identifier) right: (identifier))) right: (binary_expression left: (decimal_integer_literal) right: (identifier))) right: (binary_expression left: (identifier) right: (identifier))) right: (binary_expression left: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)) right: (character_literal)))) consequence: (block (if_statement condition: (parenthesized_expression (binary_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)))) 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: (identifier) name: (identifier) arguments: (argument_list (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)))))))