Go: package main

import (
	"fmt"
	"math"
)

type Position struct {
	X int64
	Y int64
}

func (one Position) Distance(another Position) int64 {
	return int64(math.Abs(float64(one.X-another.X)) + math.Abs(float64(one.Y-another.Y)))
}

type PersonPosition struct {
	Takayuki Position
	Kazuyuki Position
}

func (one PersonPosition) Distance() int64 {
	return one.Takayuki.Distance(one.Kazuyuki)
}

type Status struct {
	Person PersonPosition
	Turn   int64
}

type PQStatus []*Status

func solve(h, w int64) {
	var tx, ty, kx, ky int64
	fmt.Scan(&tx, &ty, &kx, &ky)
	tx--
	ty--
	kx--
	ky--
	m := make([][]int64, h)
	for i := range m {
		m[i] = make([]int64, w)
		for j := int64(0); j < w; j++ {
			fmt.Scan(&m[i][j])
		}
	}
	start := &Status{Person: PersonPosition{Takayuki: Position{X: tx, Y: ty}, Kazuyuki: Position{X: kx, Y: ky}}, Turn: 0}
	pq := make(PQStatus, 0)
	pq = append(pq, start)
	dx := []int64{0, 1, 0, -1}
	dy := []int64{1, 0, -1, 0}
	memo := make(map[PersonPosition]int64, 0)
	ans := int64(102)
	for len(pq) > 0 {
		now := &Status{}
		*now = *pq[0]
		pq = pq[1:]
		if ans <= now.Turn {
			continue
		}
		if _, ok := memo[now.Person]; ok {
			continue
		}
		memo[now.Person] = now.Turn

		if now.Person.Distance() == 0 && ans > now.Turn {
			ans = now.Turn
		}

		for i := 0; i < 4; i++ {
			next := &Status{}
			*next = *now
			next.Person.Kazuyuki.X += dx[i]
			next.Person.Kazuyuki.Y += dy[i]
			if next.Person.Kazuyuki.X < 0 || next.Person.Kazuyuki.X >= w ||
				next.Person.Kazuyuki.Y < 0 || next.Person.Kazuyuki.Y >= h ||
				m[next.Person.Kazuyuki.Y][next.Person.Kazuyuki.X] == 1 {
				next.Person.Kazuyuki.X -= dx[i]
				next.Person.Kazuyuki.Y -= dy[i]
			}
			next.Person.Takayuki.X -= dx[i]
			next.Person.Takayuki.Y -= dy[i]
			if next.Person.Takayuki.X < 0 || next.Person.Takayuki.X >= w ||
				next.Person.Takayuki.Y < 0 || next.Person.Takayuki.Y >= h ||
				m[next.Person.Takayuki.Y][next.Person.Takayuki.X] == 1 {
				next.Person.Takayuki.X += dx[i]
				next.Person.Takayuki.Y += dy[i]
			}
			next.Turn++
			if next.Turn <= 100 {
				pq = append(pq, next)
			}
		}
	}
	if ans == 102 {
		fmt.Println("NA")
	} else {
		fmt.Println(ans)
	}
}

func main() {
	var h, w int64
	for {
		fmt.Scan(&h, &w)
		if h == 0 && w == 0 {
			break
		}
		solve(h, w)
	}
}

C++: #include<bits/stdc++.h>
#define MP make_pair
#define F first
#define S second
using namespace std;
typedef pair<int, int> PII;
typedef pair<PII, PII> PPP;

int x, y;
bool visited[51][51][51][51];
bool flr[52][52];
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int main()
{
  while(true){
    cin >> x >> y;
    if(!x && !y){
      break;
    }

    int tx, ty, kx, ky;
    memset(visited, false, sizeof visited);
    memset(flr, false, sizeof flr);
    cin >> tx >> ty >> kx >> ky;

    for(int i = 1; i <= y; i++){
      for(int j = 1; j <= x; j++){
	int in;
	cin >> in;
	flr[i][j] = (bool)in;
      }
    }

    for(int i = 1; i <= y; i++){
      flr[i][0] = flr[i][x + 1] = true;
    }
    for(int i = 1; i <= x; i++){
      flr[0][i] = flr[y + 1][i] = true;
    }

    queue<PPP> q;
    q.push(MP(MP(ty, tx), MP(ky, kx)));

    for(int i = 0; i <= 100; i++){
      int sz = q.size();
      for(int j = 0; j < sz; j++){
	PPP now = q.front();
	q.pop();
	if(!visited[now.F.F][now.F.S][now.S.F][now.S.S]){
	  visited[now.F.F][now.F.S][now.S.F][now.S.S] = true;
	  
	  if(now.F.F == now.S.F && now.F.S == now.S.S){
	    cout << i << endl;
	    goto end;
	  }
	  
	  for(int k = 0; k < 4; k++){
	    int ntdx = dx[k], ntdy = dy[k], nkdx = -dx[k], nkdy = -dy[k];
	    PPP next = MP(MP(now.F.F + ntdy * !flr[now.F.F + ntdy][now.F.S + ntdx], now.F.S + ntdx * !flr[now.F.F + ntdy][now.F.S + ntdx]), MP(now.S.F + nkdy * !flr[now.S.F + nkdy][now.S.S + nkdx], now.S.S + nkdx * !flr[now.S.F + nkdy][now.S.S + nkdx]));
	    cout << now.F.F + ntdx * !flr[now.F.F + ntdx][now.F.S + ntdy] << " " << now.F.S + ntdy * !flr[now.F.F + ntdx][now.F.S + ntdy] << " " << now.S.F + nkdx * !flr[now.S.F + nkdx][now.S.S + nkdy] << " " << now.S.S + nkdy * !flr[now.S.F + nkdx][now.S.S + nkdy] << endl;
	    q.push(next);
	  }
	}
      }
    }
    cout << "NA" << endl;
  end:;
  }
  return 0;
}

C++: #include <iostream>
#include <queue>
#define rep2(x,from,to) for(int x=(from);(x)<(to);(x)++)
#define rep(x,to) rep2(x,0,to)
using namespace std;
bool mem[52][52][52][52] = {{{{}}}};
struct S{
	int a,b,c,d,t;
};
int dx[4] = {-1,0,1,0};
int dy[4] = {0,1,0,-1};
int main(){
	int x, y, tx, ty, kx, ky, k;
	while(cin >> x >> y, (x || y)) {
		int b[52][52] = {};
		rep(i,52) {
			rep(j,52) {
				rep(l,52) {
					rep(m,52) {
						mem[i][j][l][m] = 0;
					}
				}
			}
		}
		cin >> tx >> ty >> kx >> ky;
		rep(i,y) {
			rep(j,x) {
				cin >> k;
				b[i+1][j+1] = 1-k;
			}
		}
		queue<S> q;
		S s = {tx,ty,kx,ky,0};
		q.push(s);
		while(!q.empty()) {
			S s = q.front();
			if(q.front().t >= 100) break;
			if(s.a==s.c&&s.b==s.d) break;
			q.pop();
			if(mem[s.a][s.b][s.c][s.d]++) continue;
			rep(i,4) {
				int na = s.a+dx[i], nb = s.b+dy[i], nc = s.c+dx[(i+2)%4], nd = s.d+dy[(i+2)%4];
				if(!b[nb][na]&&!b[nd][nc]) continue;
				if(!b[nb][na]) na = s.a, nb = s.b;
				if(!b[nd][nc]) nc = s.c, nd = s.d;
				S ns = {na, nb, nc, nd, s.t+1};
				q.push(ns);
			}
		}
		if(q.empty()) cout << "NA" << endl;
		else if(q.front().t >= 100) cout << "NA" << endl;
		else cout << q.front().t << endl;
	}
}