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;
int main(){
  while(1){
  long long n,m;
    cin >> n >> m;
    if(n==0ll and m==0ll) break;
    char c[n][m];
    string s;
    map<char, pair<int, int> > mp;
    for(int i=0;i<n;i++){
      for(int j=0;j<m;j++){
        cin >> c[i][j];
        mp[c[i][j]]=make_pair(i,j);
      }
    }
    cin >> s;
    long long x=0,y=0,ans=0;
    for(size_t i=0;i<s.size();i++){
      ans+=abs(mp[s[i]].first-x)+abs(mp[s[i]].second-y)+1;
      x=mp[s[i]].first;
      y=mp[s[i]].second;
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include<bits/stdc++.h>
using namespace std;
int n,m,a,b,ans;
string t,s[50];
int main(void){
    while(1){
    cin>>n>>m;
    if(n==0&&m==0)return 0;
    map<char,int> mpx,mpy;
    for(int i=0;i<n;i++){
        cin>>s[i];
        for(int j=0;j<m;j++){
            if(s[i][j]=='_')continue;
            mpx[s[i][j]]=i;
            mpy[s[i][j]]=j;
        }
    }
    a=0;b=0;
    ans=0;
    cin>>t;
    for(int i=0;i<t.size();i++){
        ans+=abs(mpx[t[i]]-a)+abs(mpy[t[i]]-b);
        a=mpx[t[i]],b=mpy[t[i]];
    }
    cout<<ans+t.size()<<endl;
    }
}

C++: #include <iostream>
using namespace std;
int main(void){
  for(;;){
    int h, w;
    cin >> h >> w;
    if(h == 0 && w == 0){ return 0; }
    string r[h];
    for(int i=0;i<h;i++){
      cin >> r[i];
    }
    string s;
    cin >> s;

    // Constructing a mapping from character to coordiante:
    // c |-> (x[c], y[c]) 
    int x[128]; int y[128];
    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	x[r[i][j]] = i;
	y[r[i][j]] = j;
      }
    }

    int tempx = 0; int tempy = 0;
    int d=0;
    for(int i=0;i<s.length();i++){
      d += abs(x[s[i]] - tempx) + abs(y[s[i]] - tempy) + 1;
      tempx = x[s[i]]; tempy = y[s[i]];
    }

    cout << d << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
typedef double D;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
 
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d %d",&x,&y)
#define rl(x) scanf("%lld",&x)
#define rl2(x,y) scanf("%lld %lld",&x,&y)
#define wd(x) printf("%d" ,x)
#define wd2(x,y) printf("%d %d",x,y)
#define wl(x) printf("%lld",x)
#define wl2(x,y) printf("%lld %lld",x,y)
#define PC(x) putchar(x)
#define GC() getchar()
#define NL printf("\n")
#define SP printf(" ")
 
#define F first
#define S second
#define MP make_pair
#define PB push_back

#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL)
#define fr(i,s,e) for(int i=s;i<e;i++)
#define frl(i,s,e) for(ll i=s;i<e;i++)
#define frr(i,s,e) for(int i=s-1;i>=e;i--)
#define frv(i,a) for(int i = 0;i<(int)a.size();i++)
#define frvr(i,a) for(int i = a.size()-1;i>=0;i--)
#define tr(i,a) for(typeof(a.begin()) i = a.begin(); i != a.end();i++)

#define mod 1000000007
#define maxn 100009
#define maxr 100009
int main(){
	ll h,w;
	while(rl2(h,w),h,w)
	{
		if(h==0&&w==0)
			break;
		char key[50][50];
		map<char,PII>mp;
		fr(i,0,h)
			fr(j,0,w)
			{
				cin>>key[i][j];
				mp[key[i][j]]=MP(i,j);
			}
		string str;
		cin>>str;
		ll ct=0;
		PII pk=MP(0,0);
		fr(i,0,str.size())
		{
			PII tp=mp[str[i]];
			ct+=abs(pk.F-tp.F)+abs(pk.S-tp.S)+1;
			pk=tp;
		}
		wl(ct);
		NL;
	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<int,int> P;
const LL mod=1000000007;
const LL LINF=1LL<<60;
const int INF=1<<30;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};



int main(){
    int h,r;
    while(cin >> h >> r,h){
        vector<string> s(h);
        for (int i = 0; i < h; i++) {
            cin >> s[i];
        }
        string t;cin >> t;
        int y=0,x=0;
        int ans=0;
        for (int k = 0; k < t.length(); k++) {
            bool f=false;
            for (int i = 0; i < h; i++) {
                if(f) break;
                for (int j = 0; j < r; j++) {
                    if(s[i][j]==t[k]){
                        ans+=abs(i-y)+abs(j-x);
                        x=j,y=i;
                        f=true;
                        break;
                    }
                }
            }
        }
        cout << ans+t.length() << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using Int = long long;
int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(9), 0);
//int SEGV = getenv("D") || (exit(system("D= SEGFAULT_SIGNALS=all catchsegv ./prog.exe") >> 8), 0);
int main() {
    for (;;) {
        int H, W; cin >> H >> W;
        if (H == 0 && W == 0) break;
        vector<string> R(H); for (auto &r : R) cin >> r;
        map<char, pair<int, int>> M;
        for (int i = 0; i < H; i++) {
            for (int j = 0; j < W; j++) {
                M[R[i][j]] = make_pair(i, j);
            }
        }
        string S; cin >> S;
        int i = 0, j = 0;
        int ans = 0;
        for (auto &c : S) {
            int k, l; tie(k, l) = M[c];
            ans += abs(i - k) + abs(j - l) + 1;
            i = k, j = l;
        }
        cout << ans << '\n';
    }
    return 0;
}

C++: //ICPC B
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const ll mod=1e9+7;

int main(){
    vector<int> answers;
    while(1){
        int h,w;
        cin>>h>>w;
        if(h==0&&w==0) break;
        string r;
        pair<int,int> pos[256];
        for(int i=0;i<h;i++){
            cin>>r;
            for(int j=0;j<w;j++){
                if(r[j]!='_') pos[r[j]]={i,j};
            }
        }
        string s;
        cin>>s;
        int x=0,y=0;
        int ans=s.length();
        for(int i=0;i<s.length();i++){
            ans+=abs(y-pos[s[i]].first)+abs(x-pos[s[i]].second);
            y=pos[s[i]].first;
            x=pos[s[i]].second;
        }
        answers.push_back(ans);
    }

    for(auto ans:answers){
        cout<<ans<<endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int (i)=0;i < (n);(i)++)
#define ll long long

using namespace std;

// input
int h,w;
char field[52][52];
string query;


int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  
  while(true) {
    cin >> h >> w;
    if(h == 0) break;
    rep(i,h) rep(j,w) cin >> field[i][j];
    cin >> query;
    
    int p_x = 0,p_y = 0;
    int ans = 0;
    rep(i,query.length()) {
      char target = query[i];
      rep(y,h) rep(x,w) {
        if(field[y][x] == target) {
          ans += abs(x-p_x) + abs(y - p_y) + 1;
          p_y = y; p_x = x;
        }
      }
    }
    cout << ans << endl;
  }

}