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 <bits/stdc++.h>
using namespace std;
/*{{{*/  //template
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
constexpr int INF = numeric_limits<int>::max()/2;
constexpr long long LINF = numeric_limits<long long>::max()/3;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define sz(x) (int)(x).size()
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
//struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os, const map<T,U>& mp){ os << "{"; for(auto& p : mp){ os << p << ","; } os << "}"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
template<typename T> ostream& operator<<(ostream& os,const set<T>& st){ os<<"{"; for(T v:st) os<<v<<","; os <<"}"; return os; }
template<typename T,typename U> inline void chmax(T &x,U y){ if(y>x) x = y; }
template<typename T,typename U> inline void chmin(T &x,U y){ if(y<x) x = y; }
using ll = long long;
using ull = unsigned long long;
using pii = std::pair<int,int>;
using vi = std::vector<int>;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
//constexpr double eps = 1e-14; 
constexpr double eps = 1e-10; 
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/

int main(){
    while(1){
        int h,w; cin >> h >> w;
        if(h == 0 and w == 0) break;

        map<char,pii> mp;
        rep(i,h){
            string s; cin >> s;
            rep(j,w){
                mp[s[j]] = pii(i,j);
            }
        }

        string s; cin >> s;
        int n = s.size();

        int x,y;
        x = 0;
        y = 0;
        int ans = 0;
        rep(i,n){
            int nx = mp[s[i]].first;
            int ny = mp[s[i]].second;
            ans += abs(nx-x);
            ans += abs(ny-y);
            x = nx;
            y = ny;
        }
        ans += s.size();
        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <math.h>
using namespace std;
typedef long long int ll;

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n,m;
	while(1){
		cin >> n >> m;
		if(n==0&&m==0)break;
		pair<int,int> p[100];
		vector<string> s(n);
		for(int i=0;i<n;i++){
			cin >> s[i];
		}
		string t; cin >> t;
		// 座圧パート
		vector<char> v;
		for(int i=0;i<t.size();i++){
			v.push_back(t[i]);
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(s[i][j]!='_')v.push_back(s[i][j]);
			}
		}
		sort(v.begin(),v.end());
		v.erase(unique(v.begin(),v.end()),v.end());
		// 座圧終了
		vector<int> a(t.size());
		for(int i=0;i<t.size();i++){
			a[i]=(lower_bound(v.begin(),v.end(),t[i])-v.begin());
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(s[i][j]!='_'){
					p[lower_bound(v.begin(),v.end(),s[i][j])-v.begin()]=make_pair(i,j);
				}
			}
		}
		int ans=p[a[0]].first+p[a[0]].second+1;
		for(int i=1;i<t.size();i++){
			ans+=abs(p[a[i]].first-p[a[i-1]].first)+abs(p[a[i-1]].second-p[a[i]].second)+1;
		}
		cout << ans << endl;
	}
}

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

int main(){
    int H,W;
    while(cin >> H >> W && H){
        map<char,pair<int,int>> m;
        char c;
        for(int i=1;i<=H;i++){
            for(int j=1;j<=W;j++){
                cin >> c;
                m[c] = {i,j}; 
            }
        }
        string S;
        cin >> S;
        int ans = (int) S.size();
        int x = 1,y = 1;
        for(auto a:S){
            ans += abs(x-m[a].first)+abs(y-m[a].second);
            x = m[a].first; y = m[a].second;
        }
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define REP(i,n) for(ll i=0;i<n;i++)
#define FOR(i,n1,n2) for(ll i=n1;i<n2;i++)
#define bFOR(i,n1,n2) for(ll i=n1;i>=n2;i--)
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
typedef long long int ll;
typedef pair<ll,ll> Pi;
typedef tuple<ll,ll,ll> Tu;
const int INF=(ll)(1LL<<30)-1;
const double INFd=100000000000.0;
const double PI=3.14151926535;
const ll INFl=(ll)9223372036854775807;
const int MAX=10000;
const ll MOD=(ll)1e9+7;
const ll tMOD=(ll)998244353;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a, b)*b;}
int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};
int mdx[8]={0,1,0,-1,1,1,-1,-1},mdy[8]={-1,0,1,0,1,-1,1,-1};


template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int main(){
    while(1){
        int h,w;
        cin>>h>>w;
        if(h+w==0)break;
        Pi c[1000];
        REP(i,h){
            REP(j,w){
                char t;
                cin>>t;
                if(t=='_')continue;
                c[t]=Pi{i,j};
            }
        }
        int ans=0;
        string s;
        cin>>s;
        int t1=0,t2=0;
        REP(i,s.size()){
            ans+=(abs(c[s[i]].first-t1)+abs(c[s[i]].second-t2));
            t1=c[s[i]].first;
            t2=c[s[i]].second;
        }
        cout<<ans+s.size()<<endl;
    }
    return 0;
}