loop do
  h, w = gets.split.map(&:to_i)
  break if h == 0
  position = {}
  (0..h-1).each do |i|
    gets.chomp.each_char.with_index do |c, j|
      next if c == ?_
      position[c] = [i, j]
    end
  end
  i = j = k = 0
  s = gets.chomp.each_char do |c|
    y, x = position[c]
    k += (i - y).abs + (j - x).abs + 1
    i, j = y, x
  end
  p k
end
