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 <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

bool solve() {
  int h, w;
  cin >> h >> w;
  if (h == 0 && w == 0) {
    return false;
  }
  vector<string> r(h);
  for (auto &i : r) {
    cin >> i;
  }
  string s;
  cin >> s;
  int p = 0, q = 0, result = 0;
  for (char c : s) {
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (c == r[i][j]) {
          result += abs(p - i) + abs(q - j) + 1;
          p = i;
          q = j;
        }
      }
    }
  }
  cout << result << endl;
  return true;
}

int main() {
  while (solve()) {
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
const ll mod = 1e9 + 7;
const ll p = 137;
int h, w;
char g[55][55], s[1005];
int x[305], y[305];
int main() {
    while(scanf("%d%d", &h, &w) != EOF) {
        if(h == 0 && w == 0) break;
        for(int i = 0; i < h; ++i) {
            scanf("%s", g[i]);
            for(int j = 0; j < w; ++j) {
                int id = (int)g[i][j];
                x[id] = i, y[id] = j;
            }
        }
        scanf("%s", s);
        int len = strlen(s);
        int px = 0, py = 0, ans = 0;
        for(int i = 0; i < len; ++i) {
            int id = (int) s[i];
            ans += abs(px - x[id]) + abs(py - y[id]) + 1;
            px = x[id], py = y[id];
        }
        printf("%d\n", ans);
    }
    return 0;
}

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

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

        vector<int> posy(256), posx(256);
        for(int i=0; i<h; i++){
            string s;
            cin >> s;
            for(int j=0; j<w; j++){
                posy[s[j]] = i;
                posx[s[j]] = j;
            }
        }
        string s;
        cin >> s;
        int y=0, x=0;
        int ans = 0;
        for(char c: s){
            int p = c;
            ans += abs(posy[p]-y);
            ans += abs(posx[p]-x);
            ans++;
            y = posy[p];
            x = posx[p];
        }
        cout << ans << endl;
    }
    return 0;
}

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

int h, w;
char a[51][51], b[1001];

int main()
{
	cin >> h >> w;

	while (h != 0 && w != 0) {
		int t = 0, pi = 0, pj = 0;

		for (int i = 0; i < h; i++)
			for (int j = 0; j < w; j++)
				cin >> a[i][j];

		cin >> b;

		for (int k = 0; b[k] != '\0'; k++) {
			bool f = 0;
			for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++) {
					if (a[i][j] == b[k]) {
						t += abs(pi - i) + abs(pj - j) + 1;
						pi = i;
						pj = j;
						f = 1;
						break;
					}
				}
				if (f)
					break;
			}
		}

		cout << t << endl;

		cin >> h >> w;
	}

	return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <climits>
using namespace std;

static int mx[CHAR_MAX];
static int my[CHAR_MAX];
static char s[1024];


int main(int argc, char* argv[])
{
	for (;;) {
		int h, w;
		int x, y;
		int result;

		scanf(" %d %d", &h, &w);
		if (w == 0 && h == 0) {
			break;
		}
		for (int i = 0; i < h; ++i) {
			char r[64];

			scanf(" %s", r);
			for (int j = 0; r[j] != '\0'; ++j) {
				mx[r[j]] = j;
				my[r[j]] = i;
			}
		}

		scanf(" %s", s);
		x = y = 0;
		result = 0;
		for (int i = 0; s[i] != '\0'; ++i) {
			result += abs(mx[s[i]] - x) + abs(my[s[i]] - y) + 1;
			x = mx[s[i]];
			y = my[s[i]];
		}
		printf("%d\n", result);
	}

	return EXIT_SUCCESS;
}

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

int main() {
	int h, w;
	while (cin >> h >> w, h && w) {
		vector<string> r(h);
		for (string& i : r) cin >> i;
		string s;
		cin >> s;
		int ans = s.size();
		pair<int, int> a;
		for (char& i : s) {
			pair<int, int> b;
			for (int j = 0; j < h; ++j) {
				for (int k = 0; k < w; ++k) {
					if (i == r[j][k]) {
						b.first = j;
						b.second = k;
					}
				}
			}
			ans += abs(a.first - b.first) + abs(a.second - b.second);
			a = b;
		}
		cout << ans << endl;
	}
}

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

using Int = long long;
using ll = long long;

template<class T1, class T2> inline void chmin(T1 &a, T2 b){if(a > b) a = b;}
template<class T1, class T2> inline void chmax(T1 &a, T2 b){if(a < b) a = b;}

int dp[52][52][1010];
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int h,w;
  while(cin>>h>>w,h||w){
    vector<string> vs(h);
    for(int i=0;i<h;i++) cin>>vs[i];
    string st;
    cin>>st;

    memset(dp,-1,sizeof(dp));
    using T = tuple<int, int, int>;
    queue<T> que;
    dp[0][0][0]=0;
    que.emplace(0,0,0);

    int dy[]={0,0,1,-1};
    int dx[]={1,-1,0,0};
    auto in=[&](int y,int x){return 0<=y&&y<h&&0<=x&&x<w;};

    while(!que.empty()){
      T cur=que.front();que.pop();
      int y,x,p;
      tie(y,x,p)=cur;

      if(vs[y][x]==st[p]){
        int ny=y,nx=x,np=p+1;
        if(!in(ny,nx)) continue;
        if(~dp[ny][nx][np]) continue;
        dp[ny][nx][np]=dp[y][x][p]+1;
        if(np==(int)st.size()){
          cout<<dp[ny][nx][np]<<endl;
          break;
        }
        que.emplace(ny,nx,np);
      }

      for(int k=0;k<4;k++){
        int ny=y+dy[k],nx=x+dx[k],np=p;
        if(!in(ny,nx)) continue;
        if(~dp[ny][nx][np]) continue;
        dp[ny][nx][np]=dp[y][x][p]+1;
        que.emplace(ny,nx,np);
      }
    }
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll inf = 100100100100100;
const ll mod = 1000000007;
int dy[4]={-1,0,1,0},dx[4]={0,-1,0,1};
typedef pair<int,int> P;
#define fi first
#define se second
int main(){
int n,m;
   while(cin>>n>>m){
   if(n==0||m==0)return 0;
   vector<vector<char>> a(n,vector<char>(m));
   for(int i=0;i<n;i++)for(int j=0;j<m;j++)cin>>a.at(i).at(j);
   string s;
   cin>>s;
   P pos={0,0};
   int count=0;
   vector<vector<int>>b(n,vector<int>(m,0));
   for(int i=0;i<s.size();i++){
        vector<vector<int>>b(n,vector<int>(m,0));
       queue<P> q;
       q.push(pos);
       while(!q.empty()){
            P np=q.front();
            q.pop();
            if(a[np.fi][np.se]==s.at(i)){pos=np;break;}
            for(int i=0;i<4;i++){
               int ny=np.fi+dy[i];
               int nx=np.se+dx[i];
               if(ny<0||ny>=n||nx<0||nx>=m)continue;
               if(b[ny][nx]!=0)continue;
               b[ny][nx]=b[np.fi][np.se]+1;
               q.push(P(ny,nx));
         }
       }
       count+=b[pos.fi][pos.se];
   }
   count+=(int)s.size();
   cout<<count<<endl;
   }
}