fn main [ const bnd := 100; const n_steps := 26501365; var lines := list_break_to_lines(read_lazy(h[0])); const x := len(lines[0]); const y := len(lines); var a := list_to_array([x, y], list_join(lines, "")); var reached := array_fill(false, [ (2 * bnd + 1) * x, (2 * bnd + 1) * y ]); var n_reached := array_fill(0, [ 2 ]); var start_x := -1; var start_y := -1; for j := 0 to y do [ for i := 0 to x do [ if a[i, j] = 'S' then [ a[i, j] := '.'; start_x := bnd * x + i; start_y := bnd * y + j; ] ] ] var steps := 0; var queue := [ mktuple2(start_x, start_y) ]; var last_reached := 0; var last_diff := 0; var last_diff2 := 0; while true do [ var next_queue := empty(tuple2(int, int)); while len_greater_than(queue, 0) do [ var xp := queue[0].v1; var yp := queue[0].v2; queue := queue[1 .. ]; if not reached[xp, yp] then [ reached[xp, yp] := true; n_reached[(xp xor yp) and 1] += 1; for d in [ [0,-1], [-1,0], [0,1], [1,0] ] do [ var xn := xp + d[0]; var yn := yp + d[1]; if a[xn mod x, yn mod y] = '.', not reached[xn, yn] then next_queue +<= mktuple2(xn, yn); ] ] ] queue := next_queue; if steps = n_steps then [ write(h[1], ntos(n_reached[(start_x xor start_y xor n_steps) and 1]) + nl); return; ] if (n_steps - steps) mod (x + y) = 0 then [ var reached := n_reached[(start_x xor start_y xor n_steps) and 1]; var diff := reached - last_reached; var diff2 := diff - last_diff; if last_diff2 = diff2 then [ var todo := (n_steps - steps) div (x + y); write(h[1], ntos(reached + todo * diff + todo * (todo + 1) div 2 * diff2) + nl); return; ] last_reached := reached; last_diff := diff; last_diff2 := diff2; ] steps += 1; ] ]