require 'matrix'

loop do
    n = gets.to_i
    break if n == 0
    s, t, b = gets.chomp.split.map{|c| c.ord - ?A.ord}

    d = [2, 1, 2,
         1, 0, 1,
         2, 1, 2]
    d[b-3] += 1 if b > 2
    d[b-1] += 1 if b % 3 > 0
    d[b+1] += 1 if b % 3 < 2
    d[b+3] += 1 if b < 6

    m = Matrix.build(9, 9) do |i, j|
        if j == i
            d[i] / 4.0
        elsif j == b
            0
        elsif [1, 3].include?((i - j).abs)
            0.25
        else
            0
        end
    end

    p (m ** n)[s, t]
end