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 (pq PQStatus) Len() int {
	return len(pq)
}
func (pq PQStatus) Less(i, j int) bool {
	if pq[i].Turn == pq[j].Turn {
		return pq[i].Person.Distance() < pq[j].Person.Distance()
	}
	return pq[i].Turn < pq[j].Turn
}

func (pq PQStatus) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
}

func (pq *PQStatus) Push(x interface{}) {
	item := x.(*Status)
	*pq = append(*pq, item)
}

func (pq *PQStatus) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	*pq = old[0 : n-1]
	return item
}

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)
	//heap.Init(&pq)
	//heap.Push(&pq, start)
	//pq.Push(start)
	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 pq.Len() > 0 {
		//now := heap.Pop(&pq).(*Status)
		//now := pq.Pop().(*Status)
		now := pq[0]
		pq = pq[1:]
		if now.Person.Distance() == 0 && ans > now.Turn {
			ans = now.Turn
		}
		if ans <= now.Turn {
			continue
		}

		if _, ok := memo[now.Person]; ok {
			continue
		}
		memo[now.Person] = 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]
			}
			if now==next {
				continue
			}
			next.Turn += 1
			if next.Turn <= 100 {
				//heap.Push(&pq, next)
				//pq.Push(next)
				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<iostream>
#include<queue>
#include<vector>
#define rep(i,n) for(i = 0;i < n;i++)
using namespace std;
typedef pair<int,int> P;
typedef pair<P,P> PP;
typedef pair<PP,int> PPI;
const int INF = 10000000;

int X,Y;
int X1,Y1;
int X2,Y2;
int data[52][52];			//行,列でのデータ
int dp[52][52][52][52];		//T君が(Y,X) , K君が(Y,X)に来る時の最短手数。( 座標は(行,列) )
queue<PPI> que;

P add( P p1,P p2 ){
	p1.first += p2.first;
	p1.second += p2.second;
	return p1;
}

int BFS()
{
	PPI now;
	P pos1,pos2,next1,next2;
	int cnt;
	int dx[4] = {0,1,0,-1};
	int dy[4] = {1,0,-1,0};

	que.push( PPI( PP( P( Y1,X1 ),P( Y2,X2 ) ),0 ) );
	while( !que.empty() )
	{
		now = que.front();
		pos1 = now.first.first;
		pos2 = now.first.second;
		cnt  = now.second;
		que.pop();
		//確定
		dp[pos1.first][pos1.second][pos2.first][pos2.second] = cnt;
		//どちらも行けない場所または遷移したことがある⇒continue
		//pos1を固定すると行けるか…
		//枝を出す.  
		for( int i = 0;i < 4;i++ ){
			next1 = add( pos1 , P(dy[i],dx[i]) );
			next2 = add( pos2 , P(dy[(i+2)%4],dx[(i+2)%4]) );
			if( data[ next1.first ][ next1.second ] == 1 )
				next1 = pos1;
			if( data[ next2.first ][ next2.second ] == 1 )
				next2 = pos2;
			if( dp[ next1.first ][ next1.second ][ next2.first ][ next2.second ] > cnt+1 )
			{
				dp[ next1.first ][ next1.second ][ next2.first ][ next2.second ] = cnt+1;
				que.push( PPI( PP( next1,next2 ),cnt+1 ) );
			}
		}
	}
	int ans = INF;
	int i,j;
	rep(i,Y+2){
		rep(j,X+2){
			ans = min( ans,dp[i][j][i][j] );
		}
	}
	return ans;
}

int main(){
	int i,j,k,l;
	while( cin >> X >> Y ){
		if( X == 0 && Y == 0 )
			break;
		//dataの番兵
		rep(i,Y+2){
			rep(j,X+2){
				if(i == 0 || i == Y+1 || j == 0 || j == X+1)
					data[i][j] = 1;
				else
					data[i][j] = 0;
			}
		}
		//入力
		cin >> X1 >> Y1 >> X2 >> Y2;
		rep(i,Y){
			rep(j,X){
				cin >> data[i+1][j+1];
			}
		}
		//DPテーブルの初期化っぽいこと
		rep(i,Y+2){
			rep(j,X+2){
				rep(k,Y+2){
					rep(l,X+2){
						dp[i][j][k][l] = INF;
					}
				}
			}
		}
		//最短手数は?
		int ret = BFS();
		if( ret >= 100 )
			cout << "NA" << endl;
		else
			cout << ret << endl;
		//queを空っぽにする
		while(!que.empty())
			que.pop();
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <stack>
#include <queue>
#include <cstring>
#include <list>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
static const double EPS = 1e-5;
static const int INFTY = (1<<21);
static const ll LLINFTY = (1LL<<53);
#define REP(i,m,n) for(i=m;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin();i!=(c).end();++i)
#define out(a,b) (a<0||a>=b)
struct node{
  int tx,ty;
  int kx,ky;
  int c;
  int mini;
  node(){}
  node(int a,int b,int c_,int d,int e):tx(a),ty(b),kx(c_),ky(d),c(e),
  mini((abs(a-c_)+abs(b-d))*2){}
  bool operator>(const node& e)const{
    //return c+mini>e.c+e.mini;
    return c>e.c;
  }
};
bool used[50][50][50][50];
int dx[]={0,1,0,-1};
int dy[]={0,-1,0,1};
int main(void)
{
  int w,h,i,j,ii,jj;
  int tx,ty,kx,ky;
  int g[50][50];
  node p;
  for(;;){
    scanf("%d%d",&w,&h); if(!w)break;
    scanf("%d%d%d%d",&tx,&ty,&kx,&ky); tx--; ty--; kx--; ky--;
    rep(i,h)rep(j,w){
      scanf("%d",g[i]+j);
      rep(ii,h)rep(jj,w)used[i][j][ii][jj]=false;
    }
    priority_queue<node,vector<node>,greater<node> >q;
    q.push( node(tx,ty,kx,ky,0) );
    used[ty][tx][ky][kx]=true;
    while( !q.empty() ){
      p=q.top(); q.pop();
      if(p.tx==p.kx && p.ty==p.ky){
        printf("%d\n",p.c);
        goto END;
      }
      rep(i,4){
        tx=p.tx+dx[i]; ty=p.ty+dx[3-i];
        kx=p.kx+dy[i]; ky=p.ky+dy[3-i];
        if(out(tx,w)||out(ty,h)||g[ty][tx])tx=p.tx,ty=p.ty;
        if(out(kx,w)||out(ky,h)||g[ky][kx])kx=p.kx,ky=p.ky;
        if(!used[ty][tx][ky][kx]){
          used[ty][tx][ky][kx]=true;
          q.push(node(tx,ty,kx,ky,p.c+1));
        }
      }
    }
    puts("NA");
  END:;
  }
}