Under construction

Still being built. Things may change

Grid Game

Time limit2sMemory limit1024 MB

Summary
Two players alternate moving along right and down paths on a grid of XOR-modified values, and the task is to decide which player wins with optimal play.
Level

Hard9 of 10

Topics
Game theory, Bit manipulation, Dynamic programming, Matrix
Solved
No attempts yet

Problem

A grid AA of size N×MN \times M is given. Rows are numbered 1 to NN from top to bottom, and columns are numbered 1 to MM from left to right. Cell (r,c)(r, c) is the cell at row rr and column cc.

Each cell (r,c)(r, c) holds either −1-1 or a non-negative integer Ar,cA_{r,c}. If Ar,c=−1A_{r,c} = -1, the cell is impassable. Otherwise, the cell is passable.

Two players take turns on this grid. On a turn, a player does the following.

  1. Chooses a cell with a positive integer and stands on it. Let xx be the integer on this starting cell.
  2. Chooses a non-negative integer yy with y<xy < x.
  3. While standing on cell (r,c)(r, c), replaces Ar,cA_{r,c} with Ar,c⊕x⊕yA_{r,c} \oplus x \oplus y, where ⊕\oplus is the bitwise XOR.
  4. If cell (r+1,c)(r+1, c) or cell (r,c+1)(r, c+1) is passable, the player must move to one of the passable cells of their choice, then repeat from step 3.
  5. If the player cannot move, the player leaves the grid and the turn ends.

A player who cannot play on their turn, because no cell holds a positive integer, loses, and the other player wins.

Assuming both players play optimally, determine who wins.

Input

The first line contains two integers NN and MM (1≤N,M≤5001 \le N, M \le 500). Each of the next NN lines contains MM integers Ar,cA_{r,c} (0≤Ar,c≤1090 \le A_{r,c} \le 10^9 or Ar,c=−1A_{r,c} = -1).

Output

If the first player wins, print first on a single line. Otherwise, print second on a single line.

Examples3

  1. Example 1

    Input
    5 6
    0 0 0 0 0 0
    0 3 3 0 0 0
    0 0 3 -1 0 0
    0 0 3 3 3 3
    0 0 -1 -1 -1 -1
    
    Expected output
    first
    
  2. Example 2

    Input
    2 2
    1 1
    2 -1
    
    Expected output
    first
    
  3. Example 3

    Input
    1 1
    -1
    
    Expected output
    second