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 <vector>
#include <algorithm>
#include <string>
#include <numeric>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
typedef long long int ll;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
    // clang-format on

#include <utility>
using P = pair<int, int>;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int h, w;
    while (cin >> h >> w, h + w) {
        P pos[128];
        REP(i, 0, h) {
            string s;
            cin >> s;
            REP(j, 0, s.size()) {
                pos[s[j]] = make_pair(i, j);
            }
        }
        string s;
        cin >> s;
        P p(0, 0);
        int ans = 0;
        for (auto c : s) {
            ans += abs(p.first - pos[c].first) + abs(p.second - pos[c].second) + 1;
            p = pos[c];
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
using namespace std;

int main(){
    int h, w;
    while(cin >> h >> w, h){
        vector<string> r(h);
        for(int i = 0 ; i < h ; ++i){
            cin >> r[i];
        }

        string s;
        cin >> s;

        int y = 0, x = 0;
        int ans = 0;
        for(auto c : s){
            for(int i = 0 ; i < h ; ++i){
                for(int j = 0 ; j < w ; ++j){
                    if(r[i][j] == c){
                        ans += abs(i - y) + abs(j - x) + 1;
                        y = i;
                        x = j;
                    }
                }
            }
        }

        cout << ans << endl;
    }

    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
static const int INFTY = 2501;
string imap[51];
string str;
int d[51][51];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int h, w;
int x, y;

int bfs(const int &idx) {
    for (int i = 0; i < h; i++) fill(d[i], d[i]+w, INFTY);
    queue<pair<int, int> > q;
    pair<int, int> p;
    d[x][y] = 0;
    int nx, ny, sum = 0;
    q.push({x, y});
    while (!q.empty()) {
        p = q.front(); q.pop();
        if (imap[p.first][p.second] == str[idx]) { x = p.first, y = p.second; sum = d[p.first][p.second]; break; }
        for (int i = 0; i < 4; i++) {
            nx = p.first+dx[i], ny = p.second+dy[i];
            if (nx >= 0 && nx < h && ny >= 0 && ny <= w && d[nx][ny] == INFTY) {
                d[nx][ny] = d[p.first][p.second]+1;
                q.push({nx, ny});
            }
        }
    }
    return sum;
}



int main(void){
    for(; ;) {
        cin >> h >> w;
        if (h == 0 && w==0) break;
        for (int i = 0; i < h; i++) {
            cin >> imap[i];
        }
        cin >> str;
        x = y = 0;
        int sum = 0;

        for (int i = 0; i < str.size(); i++) {
            if (str[i] != imap[x][y]) sum+=bfs(i)+1;
            else sum+=1;
        }
        printf("%d\n", sum);
    }
    return 0;
}

C++: // https://atcoder.jp/contests/agc037/submissions/6956246
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define chmax(a, b) a = (a >= b ? a : b)
#define chmin(a, b) a = (a <= b ? a : b)
#define sz(v) (int)v.size()
#define inf (int)(1e9+7)
#define ceil(a, b) a / b + !!(a % b)
ll pow(ll a, int b) { return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; }
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int h;
    while (cin >> h && h) {
        int w;
        cin >> w;
        char r[h][w];
        rep(i, h) rep(j, w) cin >> r[i][j];
        
        string str;
        cin >> str;
        
        int ans = 0;
        
        pii now = pii(0, 0);
        rep(k, sz(str)) {
            queue<pii> q;
            q.push(now);
            
            int reached[h][w];
            rep(i, h) rep(j, w) reached[i][j] = -1;
            reached[now.F][now.S] = 0;
            
            while (!q.empty()) {
                pii cur = q.front();
                q.pop();
                
                rep(i, 4) {
                    int ny = cur.F + dy[i];
                    int nx = cur.S + dx[i];
                    if (ny < 0 || ny >= h || nx < 0 || nx >= w || reached[ny][nx] != -1) continue;
                    reached[ny][nx] = reached[cur.F][cur.S] + 1;
                    q.push(pii(ny, nx));
                }
            }
            
            rep(i, h) rep(j, w) if (r[i][j] == str[k]) {
                ans += reached[i][j] + 1;
                now = pii(i, j);
            }
            
            // cout << endl; rep(i, h) { rep(j, w) cout << reached[i][j] << " "; cout << endl; }
            // cout << ans << endl;
        }
        
        cout << ans << endl;
    }
}

C++: #include<iostream>
#include<map>
using namespace std;

int main(){
    int h, w;
    while(cin >> h >> w, h+w){
        map<char, pair<int,int>> m;
        for(int i = 0; i < h; i++){
            for(int j = 0; j < w; j++){
                char c; cin >> c;
                if(c == '_')    continue;
                m[c] = {i,j};
            }
        }
        string s;
        cin >> s;
        int i = 0, j = 0, ans = s.length();
        for(char c : s) ans += abs(m[c].first-i)+abs(m[c].second-j), i = m[c].first, j = m[c].second;
        cout << ans << endl;
    }
    return 0;
}

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

using namespace std;

int main(void){
	while(1){
		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; //入力したい文字列

		int ans = 0;
		int pos_x=0, pos_y=0; //最後に押した(強調されている)cellの位置
		for(int s=0; s<str.length(); s++){
			for(int y=0; y<h; y++){
				bool find = false; //二重ループを抜けるためのフラグ
				for(int x=0; x<w; x++){
					if(cell[y][x] == str[s]){
						ans += abs(y-pos_y) + abs(x-pos_x) + 1; //マンハッタン距離+1
						pos_x = x; 
						pos_y = y;
						find = true;
						break;
					}
				}
				if(find) break;
			}
		}
		cout << ans << endl;
	}
	return 0;
}