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]
			}
			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<map>
#include<cstring>
#define H(tx,ty) tx<0||tx>=h||ty<0||ty>=w||MAS[tx][ty]==1
using namespace std;
int h,w,tx,ty,kx,ky,d[]={0,1,0,-1},MAS[51][51];
bool used[51][51][51][51];
struct P{
  int tx,ty,kx,ky,cnt;
  P(int tx,int ty,int kx,int ky,int cnt):tx(tx),ty(ty),kx(kx),ky(ky),cnt(cnt){}
};
int bfs();
int main(){
  while(cin >> w >> h && w|h){
    cin >> ty >> tx >> ky >> kx;
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	cin >> MAS[i][j];
    memset(used,false,sizeof(used));
    int ans = bfs();
    if(ans==-1)cout << "NA" << endl;
    else cout << ans << endl;
  }
}
int bfs(){
  queue<P>que;
  que.push(P(tx-1,ty-1,kx-1,ky-1,0));
  used[tx-1][ty-1][kx-1][ky-1] = true;
  while(!que.empty()){
    P p = que.front();
    if(p.tx == p.kx && p.ty == p.ky) return p.cnt;
    else if(p.cnt >= 100) return -1;
    for(int i=0;i<4;i++){
      int ntx = p.tx + d[i],nty = p.ty + d[3-i];
      int nkx = p.kx - d[i],nky = p.ky - d[3-i];
      if(H(ntx,nty))ntx = p.tx,nty = p.ty;
      if(H(nkx,nky))nkx = p.kx,nky = p.ky;
      if(!used[ntx][nty][nkx][nky]){
	que.push(P(ntx,nty,nkx,nky,p.cnt+1));
	used[ntx][nty][nkx][nky] = true;
      }
    }
    que.pop();
  }
  return -1;
}

C++: #include <cstdio>
#include <queue>
using namespace std;

int dy[] = {0,0,1,-1}, dx[] = {1,-1,0,0};

inline int enc(int y,int x){
    return 52*y+x;
}

inline int enc2(int ty, int tx, int ky, int kx){
    return enc(ty,tx)*52*52 + enc(ky,kx);
}

int main(){
    int w,h,tx,ty,kx,ky;
    static int board[52][52];
    static int dist[52*52*52*52];
    while(scanf("%d%d",&w,&h),w){
        int ans = 1<<20;
        queue< pair<int,int> > Q;
        scanf("%d%d%d%d",&tx,&ty,&kx,&ky);
        for(int i=0; i<52; i++)
            for(int j=0; j<52; j++)
                board[i][j] = 1;
        for(int i=1; i<=h; i++)
            for(int j=1; j<=w;j++)
                scanf("%d",board[i]+j);
        for(int i=0; i<52*52*52*52; i++)
            dist[i] = 1<<10;
        if(enc(ty,tx)==enc(ky,kx)){
            printf("%d\n",0);
            goto end;
        }

        dist[enc2(ty,tx,ky,kx)] = 0;
        for(int k=0; k<4; k++){
            int nty = ty + dy[k],
                ntx = tx + dx[k],
                nky = ky - dy[k],
                nkx = kx - dx[k];
            if(board[nty][ntx]) nty = ty, ntx = tx;
            if(board[nky][nkx]) nky = ky, nkx = kx;
            if(enc(nty,ntx)==enc(nky,nkx)){
                printf("%d\n",1);
                goto end;
            }
            int p = enc2(nty,ntx,nky,nkx);
            if(1 < dist[p]){
                dist[p] = 1;
                Q.push( make_pair(p,2) );
            }
        }
        while(!Q.empty()){
            ty = (Q.front().first/(52*52))/52;
            tx = (Q.front().first/(52*52))%52;
            ky = (Q.front().first%(52*52))/52;
            kx = (Q.front().first%(52*52))%52;
            int d = Q.front().second;
            if(d > 100) break;
            Q.pop();
            for(int k=0; k<4; k++){
               int nty = ty + dy[k],
                   ntx = tx + dx[k],
                   nky = ky - dy[k],
                   nkx = kx - dx[k];
               if(board[nty][ntx]) nty = ty, ntx = tx;
               if(board[nky][nkx]) nky = ky, nkx = kx;
               if(enc(nty,ntx)==enc(nky,nkx)){
                    printf("%d\n",d);
                    goto end;
               }
               int p = enc2(nty,ntx,nky,nkx);
               if(d < dist[p]){
                   dist[p] = d;
                   Q.push( make_pair(p,d+1) );
               }
           }
        }
        printf("NA\n");
end:;
    }
}