Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

type Position = []int

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}

func main() {
	scanner.Split(bufio.ScanWords)

	for {
		h := nextInt()
		w := nextInt()

		if h == 0 && w == 0 {
			break
		}
		positions := map[string]Position{}
		for i := 0; i < h; i++ {
			row := nextString()
			for j := 0; j < w; j++ {
				pos := []int{i, j}
				positions[row[j:(j+1)]] = pos
			}
		}
		// fmt.Println(positions)

		x := 0
		y := 0
		totalDistance := 0

		queryRow := nextString()
		for i := 0; i < len(queryRow); i++ {
			q := queryRow[i:(i + 1)]

			nextPos := positions[q]
			xNext := nextPos[0]
			yNext := nextPos[1]

			totalDistance += abs(xNext-x) + abs(yNext-y) + 1
			x = xNext
			y = yNext
			// fmt.Println(nextPos, totalDistance)
		}
		fmt.Println(totalDistance)
	}
}

C++: #include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main(){
    while(1){
        int px = 0, py = 0,t = 0; 
        int h,w; cin >> h >> w;
        if(!h&&!w) break;
        string cell[55];

        for(int i = 0; i < h; i++){
            cin >> cell[i];
        }

        string str; cin >> str ;

        for(int x = 0; x < str.length(); x++){
            for(int j  = 0; j < h; j++){
                int find = 0;
                for(int i = 0; i < w; i++){
                    if(cell[j][i] == str[x]){
                        t += abs(j - py) + abs(i - px) + 1;
                        px = i;
                        py = j;
                        find = 1;
                        break;
                    }
                }
                if(find) break;
            }
        }

        cout << t << endl;

    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
          int h, w;
          int n;
          int i,j,k;
          while(1){
                    int ans=0;
                    int x=0, y=0; //座標
                    cin >> h >> w;
                    if(!h&&!w) break;
                    string keybord[55];
                    string s;
                    for(i=0;i<h;i++) cin >> keybord[i];
                    cin >> s;

                    for(i=0;i<s.length();i++){
                              for(j=0;j<h;j++){
                                     n=keybord[j].find(s[i]);
                                     if(n>=0){
                                        ans+=abs(x-n)+abs(y-j)+1;
                                        x=n;
                                        y=j;
                                        break;
                                     }
                              }   
                    } 
                    cout << ans << endl;
                       
          }
}

C++: #include <bits/stdc++.h>

#define int long long
#define REP(i, l, r) REPEAT(i,l,r,true) // [l, r)
#define rep(i, n) REP(i,0,n)           // [0, n)
#define REPEAT(i, l, r, condition) for(int i=(condition)?l:r-1;(condition)?i<r:i>=l;(condition)?++i:--i) // false<-[l, r)->true
#define all(e) e.begin(),e.end()
#define rall(e) e.rbegin(),e.rend()
#define pb push_back
#define fs first
#define sc second
#ifdef LOCAL
#define show(...) cerr<<#__VA_ARGS__<<" = ";_DEBUG(__VA_ARGS__)
#define showlr(n, l, r) cerr<<#n<<" = ";for(int i=l;i<r;i++){cerr<<n[i]<<", ";}cerr<<endl // [l, r)
#else
#define show(...) true
#define showlr(n,l,r) true
#endif

#define YN(condition) puts((condition)?"YES":"NO")
#define Yn(condition) puts((condition)?"Yes":"No")
#define yn(condition) puts((condition)?"yes":"no")
#define YES puts("YES")
#define Yes puts("Yes")
#define yes puts("yes")
#define NO  puts("NO")
#define No  puts("No")
#define no  puts("no")

#define case(i) printf("Case #%lld: ",i)

using namespace std;

using vi=vector<int>;
using pint=pair<int, int>;

struct io {
    io() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout.tie(0);
        cout << fixed << setprecision(20);
    }
} io;

template<class T>
istream &operator>>(istream &is, vector<T> &v) {
    for (T &e:v)is >> e;
    return is;
}

template<class T>
ostream &operator<<(ostream &os, vector<T> v) {
    os << "{";
    for (T &e:v)os << e << (v.size() - (int) (&e - &v[0]) > 1 ? ", " : "");
    os << "}";
    return os;
}

void _DEBUG() {}

template<typename H, typename... T>
void _DEBUG(H a, T...b) {
    cerr << a << (sizeof...(b) ? "," : "\n");
    _DEBUG(b...);
}

inline void in() {}

template<typename H, typename... T>
void in(H &a, T &... b) {
    cin >> a;
    in(b...);
}

inline void out() {}

template<typename H, typename... T>
void out(H a, T... b) {
    cout << a << (sizeof...(b) ? " " : "\n");
    out(b...);
}

template<class T>
void resz(int n, T &v) { v.resize(n); }

template<class H, class... T>
void resz(int n, H &a, T &... b) {
    a.resize(n);
    resz(n, b...);
}

template<typename V, typename H>
void resize(vector<V> &v, const H a) { v.resize(a); }

template<typename V, typename H, typename... T>
void resize(vector<V> &v, const H &a, const T... b) {
    v.resize(a);
    for (auto &v:v) resize(v, b...);
}

const int INF = 1LL << 55;
const int MOD = 1000000007;
const double EPS = 1e-8;

/*------------end of definitions------------*/

int H, W;

void solve() {
    map<char, pint> M;
    rep(i, H) {
        rep(j, W) {
            char c;
            in(c);
            M[c] = {i, j};
        }
    }
    vector<pint> V;
    V.pb({0, 0});
    string S;
    in(S);
    int ans = S.size();
    for_each(all(S), [&V, &M](char c) { V.pb(M[c]); });

    REP(i, 1, V.size()) {
        ans += abs(V[i - 1].first - V[i].first) + abs(V[i - 1].second - V[i].second);
    }
    out(ans);
}

signed main() {

    while (in(H, W), H) {
        solve();
    }

    return 0;
}

C++: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define SIZE 1005

struct LOC{

	int row,col;
};

struct Info{
	Info(int arg_row,int arg_col,char arg_ch){
		loc.row = arg_row;
		loc.col = arg_col;
		ch = arg_ch;
	}

	LOC loc;
	char ch;
};

int H,W;
int info_index[128],min_dist[128][128];
char table[55][55],buf[SIZE];
vector<Info> V;


void func(){

	V.clear();

	for(int row = 0; row < H; row++){

		scanf("%s",table[row]);
		for(int col = 0; col < W; col++){

			if(table[row][col] == '_')continue;

			info_index[table[row][col]] = V.size(); //文字からインデックスを調べる表
			V.push_back(Info(row,col,table[row][col]));
		}
	}

	scanf("%s",buf);

	for(int i = 0; i < V.size()-1; i++){
		for(int k = i; k < V.size(); k++){

			min_dist[V[i].ch][V[k].ch] = abs(V[i].loc.row-V[k].loc.row)+abs(V[i].loc.col-V[k].loc.col);
			min_dist[V[k].ch][V[i].ch] = min_dist[V[i].ch][V[k].ch];
		}
	}

	int ans = V[info_index[buf[0]]].loc.row+V[info_index[buf[0]]].loc.col+1;

	char pre = buf[0];

	for(int i = 1; buf[i] != '\0'; i++){

		ans += min_dist[pre][buf[i]]+1;
		pre = buf[i];
	}

	printf("%d\n",ans);
}

int main(){

	while(true){

		scanf("%d %d",&H,&W);
		if(H == 0 && W == 0)break;

		func();
	}

	return 0;
}