while true
    h, w = gets.chomp.split.map(&:to_i)
    break if [h, w] == [0, 0]

    keyboard = ""
    h.times{keyboard += gets.chomp}
    x, y, hit = 0, 0, 0
    gets.chomp.split("").each{|c|
        z = keyboard.index(c)
        y2, x2 = z.divmod(w)
        hit += (x - x2).abs + (y - y2).abs + 1
        x, y = x2, y2
    }
    puts hit
end
