static int matrixJump ( int M [ ] [ ] , int R1 , int C1 ) {
  Queue < pair < Integer , pair < Integer , Integer >>> q = new LinkedList < > ( ) ;
  q . add ( new pair ( 1 , new pair ( R1 , C1 ) ) ) ;
  while ( q . size ( ) > 0 ) {
    int x = q . peek ( ) . second . first ;
    int y = q . peek ( ) . second . second ;
    int no_of_cells = q . peek ( ) . first ;
    q . remove ( ) ;
    if ( x == R - 1 && y == C - 1 ) return no_of_cells ;
    int v = M [ x ] [ y ] ;
    if ( safe ( x + v , y ) ) q . add ( new pair ( no_of_cells + 1 , new pair ( x + v , y ) ) ) ;
    if ( safe ( x , y + v ) ) q . add ( new pair ( no_of_cells + 1 , new pair ( x , y + v ) ) ) ;
  }
  return - 1 ;
}

static int matrixJump ( int M [ ] [ ] , int R1 , int C1 ) { Queue < pair < Integer , pair < Integer , Integer > > > q = new LinkedList < > ( ) ; q . add ( new pair ( 1 , new pair ( R1 , C1 ) ) ) ; while ( q . size ( ) > 0 ) { int x = q . peek ( ) . second . first ; int y = q . peek ( ) . second . second ; int no_of_cells = q . peek ( ) . first ; q . remove ( ) ; if ( x == R - 1 && y == C - 1 ) return no_of_cells ; int v = M [ x ] [ y ] ; if ( safe ( x + v , y ) ) q . add ( new pair ( no_of_cells + 1 , new pair ( x + v , y ) ) ) ; if ( safe ( x , y + v ) ) q . add ( new pair ( no_of_cells + 1 , new pair ( x , y + v ) ) ) ; } return - 1 ; }
(method_declaration (modifiers) type: (integral_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (integral_type) name: (identifier) dimensions: (dimensions)) (formal_parameter type: (integral_type) name: (identifier)) (formal_parameter type: (integral_type) name: (identifier))) body: (block (local_variable_declaration type: (generic_type (type_identifier) (type_arguments (generic_type (type_identifier) (type_arguments (type_identifier) (generic_type (type_identifier) (type_arguments (type_identifier) (type_identifier))))))) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (generic_type (type_identifier) (type_arguments)) arguments: (argument_list)))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (object_creation_expression type: (type_identifier) arguments: (argument_list (decimal_integer_literal) (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: (integral_type) declarator: (variable_declarator name: (identifier) value: (field_access object: (field_access object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) field: (identifier)) field: (identifier)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (field_access object: (field_access object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) field: (identifier)) field: (identifier)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (field_access object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) field: (identifier)))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (binary_expression left: (identifier) right: (decimal_integer_literal))) right: (binary_expression left: (identifier) right: (binary_expression left: (identifier) right: (decimal_integer_literal))))) consequence: (return_statement (identifier))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (array_access array: (array_access array: (identifier) index: (identifier)) index: (identifier)))) (if_statement condition: (parenthesized_expression (method_invocation name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (identifier)) (identifier)))) consequence: (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (object_creation_expression type: (type_identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)) (object_creation_expression type: (type_identifier) arguments: (argument_list (binary_expression left: (identifier) right: (identifier)) (identifier))))))))) (if_statement condition: (parenthesized_expression (method_invocation name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (identifier) right: (identifier))))) consequence: (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (object_creation_expression type: (type_identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)) (object_creation_expression type: (type_identifier) arguments: (argument_list (identifier) (binary_expression left: (identifier) right: (identifier)))))))))))) (return_statement (unary_expression operand: (decimal_integer_literal)))))