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 <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)

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

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

typedef long long int ll;
using ll = long long int;
using ull = long long unsigned int;
using Int = long long int;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007


//edit
class Solve {
public:
    bool solve() {
        Int H, W;
        cin >> H >> W;
        if (H == 0) return false;
        map<char, pair<Int, Int>> mp;

        for (int i = 0; i < H; ++i) {
            for (int j = 0; j < W; ++j) {
                char c;
                cin >> c;
                mp[c] = make_pair(i, j);
            }
        }
        string s;
        cin >> s;
        Int ans = 0;
        Int h = 0, w = 0;
        auto dist = [&](Int nh, Int nw) {
            return abs(nh - h) + abs(w - nw);
        };

        for (auto e : s) {
            auto p = mp[e];
            Int nh = p.first;
            Int nw = p.second;
            Int d = dist(nh, nw);
            h = nh;
            w = nw;
            ans += d + 1;
        }
        cout << ans << endl;

        return true;
    }

};


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    while (Solve().solve());

    return 0;
}

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

using namespace std;

class ScreenKeyboard {
private:
  char m_key[50][50];
  int m_count;
  int m_width, m_height;
  struct Position {
    int x;
    int y;
  };
  Position m_position;

  void findKey(char key, Position &pos)
  {
    for (int i = 0; i < m_width; i++) {
      for (int j = 0; j < m_height; j++) {
        if (m_key[i][j] == key) {
          pos.x = i;
          pos.y = j;
        }
      }
    }
  }

public:
  ScreenKeyboard(int width, int height) : m_count(0), m_position(),
                                          m_width(width), m_height(height)
  {
    for (int i = 0; i < width; i++) {
      for (int j = 0; j < height; j++) {
        m_key[i][j] = '_';
      }
    }
  }

  ~ScreenKeyboard()
  {
  }

  void setKey(char key, int x, int y)
  {
    m_key[x][y] = key;
  }

  void moveKey(char key)
  {
    Position pos;
    findKey(key, pos);
    m_count += abs(m_position.x - pos.x) + abs(m_position.y - pos.y) + 1;
    m_position.x = pos.x;
    m_position.y = pos.y;
  }

  int getCount()
  {
    return m_count;
  }
};

int main()
{
  int h, w;
  
  while (1) {
    cin >> h >> w;
    if (h == 0 && w == 0) return 0;
    cin.ignore();
    ScreenKeyboard* screenKeyboard = new ScreenKeyboard(h, w);
    string keyString[50];
    string inString;
    for (int i = 0; i < h; i++) {
      getline(cin, keyString[i]);
      for (int j = 0; j < keyString[i].size(); j++) {
        screenKeyboard->setKey(keyString[i][j], i, j);
      }
    }
    getline(cin, inString);
    for (int i = 0; i < inString.size(); i++) {
      screenKeyboard->moveKey(inString[i]);
    }
    cout << screenKeyboard->getCount() << endl;
    delete screenKeyboard;
  }
}

C++: #include <stdio.h>
#include <string.h>
#include <math.h>
#define MAX 1000
int n,m;
char  a[51][51],c[MAX];
int pos[MAX][2];
int main()
{
    while(scanf("%d%d",&n,&m)&&(m||n))
    {
        for(int i=0; i<n; i++)
        {
            getchar();
            for(int j=0; j<m; j++)
                scanf("%c",&a[i][j]);
        }
        getchar();
       scanf("%s",c);
        int len=strlen(c);
        for(int i=0; i<len; i++)
        {
            for(int j=0; j<n; j++)
                for(int k=0; k<m; k++)
                    if(a[j][k]==c[i])
                    {
                        pos[i][0]=j;
                        pos[i][1]=k;
                        break;
                    }
        }
        int sum=pos[0][0]+pos[0][1];
        for(int i=1; i<len; i++)
            sum+=fabs(pos[i][0]-pos[i-1][0])+fabs(pos[i][1]-pos[i-1][1]);
            printf("%d\n",sum+len);
        }
         return 0;
}

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

typedef long long ll;
#define ALL(a) a, a+sizeof(a)/sizeof(a[0])
#define all(a) a.begin(),a.end()
#define MOD 1000000007
using namespace std;
ll gcd(ll n, ll m) {
	if (m == 0) return n;
	return gcd(m, n % m);
}
ll lcm(ll n, ll m) {return m / gcd(n, m) * n;}

int code[127][2];

int main(void) {
	int h,w,ans=0;
	string s;
	while (1) {
		ans = 0;
		int before[2] = { 0,0 };
		cin >> h >> w;
		if (h == 0 && w == 0) break;
		vector<string> r(h);
		for (int i = 0; i < h; i++) cin >> r[i];
		cin >> s;
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				code[r[i][j]][0] = i;
				code[r[i][j]][1] = j;
			}
		}
		for (int i = 0; i < s.length(); i++) {
			ans += abs(code[s[i]][0] - before[0]);
			ans += abs(code[s[i]][1] - before[1]) + 1;
			memcpy(before, code[s[i]], sizeof(before));
		}
		cout << ans << "\n";
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
int main(){
    int h,w;
    while(cin>>h>>w){
        if(h+w == 0)break;
        vector<string> v(h);
        map<char,pair<int,int>> mp;
        for(int i=0;i<h;i++)cin>>v[i];
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                mp[v[i][j]] = pair<int,int>(i,j);
            }
        }
        string s;
        cin>>s;
        pair<int,int> now = pair<int,int>(0,0);
        int ans=0;
        for(int i=0;i<s.size();i++){
            pair<int,int> next = mp[s[i]];
            ans += abs(now.first-next.first)+abs(now.second-next.second);
            now = next;
        }
        cout<<ans + s.size()<<endl;
    }
}