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>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include<sstream>
#include<iterator>
#include<list>

using namespace std;

typedef  long long int lint;

#define rep(i, n) for (lint i = 0; i < n; i++)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
#define upper(v,hoge) upper_bound(v.begin(),v.end(),hoge)
#define lower(v,hoge) lower_bound(v.begin(),v.end(),hoge)
#define llower(v,hoge) *lower_bound(v.begin(), v.end(), hoge)
#define lupper(v,hoge) *upper_bound(v.begin(), v.end(), hoge)
#define mp make_pair
#define IP pair<int,int>
#define enld endl

int main() {
	lint H, W;
	while (cin >> H >> W, H != 0 && W != 0) {
		vector<vector<char>>A(H, vector<char>(W));
		rep(i, H) {
			rep(j, W) {
				cin >> A[i][j];
			}
		}
		map<char, lint>X;
		map<char, lint>Y;
		rep(i, H) {
			rep(j, W) {
				if (A[i][j] != '_') {
					X[A[i][j]] = i;
					Y[A[i][j]] = j;
				}
			}
		}
		string S;
		cin >> S;
		lint ans = 0;
		rep(i, S.size()) {
			if (i == 0) {
				ans += X[S[i]];
				ans += Y[S[i]];
			}
			else {
				ans += abs(X[S[i - 1]] - X[S[i]]);
				ans += abs(Y[S[i - 1]] - Y[S[i]]);
			}
		}
		ans += S.size();
		cout << ans << endl;
	}
}

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

#define REP(i,n) for (int i=0;i<(n);++i)

using namespace std;

using pii = pair<int, int>;

int distance(pii p, pii q) {
    return abs(p.first - q.first) + abs(p.second - q.second);
}

int main() {
    while (true) {
        int h, w;
        cin >> h >> w;
        if (h == 0)
            break;
        map<char, pii> position;
        REP(i, h) {
            string r;
            cin >> r;
            REP(j, w) {
                position[r[j]] = pii(i, j);
            }
        }
        string s;
        cin >> s;
        int count_presses = 0;
        pii current_position = pii(0, 0);
        for (char c: s) {
            pii next_position = position[c];
            count_presses += distance(current_position, next_position) + 1;
            current_position = next_position;
        }
        cout << count_presses << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <fstream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<P> vp;
#define rep(i,a,n) for(ll i = (a);i < (n);i++)
#define per(i,a,n) for(ll i = (a);i > (n);i--)
#define lep(i,a,n) for(ll i = (a);i <= (n);i++)
#define pel(i,a,n) for(ll i = (a);i >= (n);i--)
#define clr(a,b) memset((a),(b),sizeof(a))
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define sz size()
#define endl "\n"
#define print(X) cout << (X) << "\n"
// // #define input(X) getline(cin,X)
static const ll  INF = 1e9+7;
static const ll INFL = 1e18+7;
ll n,m,l, p;
string s,t;
ll d[200010],e[200010],dp[1001][1001];
char field[202][202];

int x = 0,y = 0;

vector<P> get(char c){
	vector<P> ans;
	rep(i,0,n){
		rep(j,0,m){
			if(field[i][j] == c)ans.pb(P{i,j});
		}
	}
	return ans;
}

int calc(vector<P> vec){
	P now;
	int ans = INF;
	rep(i,0,vec.sz){
		P point = vec[i];
		int l = abs(x-point.first)+abs(y-point.second) + 1;
		if(l < ans){
			now = point;
			ans = l;
		}
	}

	x = now.first;
	y = now.second;
	return ans;
}

int solve(){
	int ans = 0;
	rep(i,0,s.sz){
		vector<P> points = get(s[i]);
		ans += calc(points);
	}
	return ans;
}

int main(){
	while(cin >> n >> m, n){
		int ans = 0;
		x = y = 0;

		rep(i,0,n)cin >> field[i];
		cin >> s;
		print(solve());
	}
	return 0;
}

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

struct data {
  int x, y, id, cnt;
};

long long h, w, n;
int d[4] = {1, 0, -1, 0};
vector<string> r;
string s;
queue<data> qu;
bool memo[55][55][1005] = {0};

bool isvalid(const data &now) {
  return now.x >= 0 && now.x < h && now.y >= 0 && now.y < w;
}

long long solve();

int main() {
  while(1) {
    cin >> h >> w;
    if(h + w == 0) break;
    r.resize(h);
    for(int i = 0; i < h; ++i) cin >> r[i];
    cin >> s;
    n = s.size();
    cout << solve() << endl;
  }
  return 0;
}

long long solve() {
  while(!qu.empty()) qu.pop();
  for(int i = 0; i < h; ++i)
    for(int j = 0; j < w; ++j)
      for(int k = 0; k <= n; ++k) memo[i][j][k] = 0;
  qu.push({0, 0, 0, 0});
  while(!qu.empty()) {
    data now = qu.front();
    qu.pop();
    ++now.cnt;
    if(r[now.x][now.y] == s[now.id]) {
      ++now.id;
      memo[now.x][now.y][now.id] = 1;
      if(now.id == n) return now.cnt;
      qu.push(now);
      continue;
    }
    for(int i = 0; i < 4; ++i) {
      data nextp = now;
      nextp.x += d[i];
      nextp.y += d[i ^ 1];
      if(isvalid(nextp) &&
         !memo[nextp.x][nextp.y][nextp.id]) {
        memo[nextp.x][nextp.y][nextp.id] = 1;
        qu.push(nextp);
      }
    }
  }
  return -1;
}

C++: #include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  while (true) {
    int h, w;
    cin >> h >> w;
    if (h == 0 && w == 0)
      break;
    vector<string> r(h);
    for (int i = 0; i < h; ++i)
      cin >> r[i];
    string s;
    cin >> s;

    map<char, pair<int, int>> m;
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j)
        if (r[i][j] != '_')
          m[r[i][j]] = make_pair(i, j);

    int pi = 0, pj = 0, ans = 0;
    for (int k = 0; k < s.size(); ++k) {
      ans += abs(m[s[k]].first - pi) + abs(m[s[k]].second - pj) + 1;
      pi = m[s[k]].first, pj = m[s[k]].second;
    }

    cout << ans << endl;
  }
}