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(101)
	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 <= 98 {
				//heap.Push(&pq, next)
				//pq.Push(next)
				pq = append(pq, next)
			}
		}
	}
	if ans == 101 {
		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<queue>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
short b[52][52][52][52];
int main(){
  int i,j;
  int a[52][52],sx,sy,tx,ty,tm,w,h;
  queue<pair<pair<pair<int,int>,pair<int,int> >,int> > q;
  while(cin>>w>>h&&w+h){
    cin>>sx>>sy>>tx>>ty;
    memset(a,-1,sizeof(a));
    for(i=1;i<h+1;i++){
      for(j=1;j<w+1;j++){
	cin>>a[j][i];
	a[j][i]=-a[j][i];
      }
    }
    memset(b,-1,sizeof(b));
    while(q.empty()==0)
      q.pop();
    q.push(make_pair(make_pair(make_pair(sx,sy),make_pair(tx,ty)),0));
    while(q.empty()==0){
      sx=q.front().first.first.first;
      sy=q.front().first.first.second;
      tx=q.front().first.second.first;
      ty=q.front().first.second.second;
      tm=q.front().second;
      if(tm>100)
	break;
      if(sx==tx&&sy==ty)
	break;
      q.pop();
      if(b[sx][sy][tx][ty]==-1){
	b[sx][sy][tx][ty]=tm;
	q.push(make_pair(make_pair(make_pair(sx-(a[sx-1][sy]?0:1),sy),make_pair(tx+(a[tx+1][ty]?0:1),ty)),tm+1));
	q.push(make_pair(make_pair(make_pair(sx+(a[sx+1][sy]?0:1),sy),make_pair(tx-(a[tx-1][ty]?0:1),ty)),tm+1));
	q.push(make_pair(make_pair(make_pair(sx,sy-(a[sx][sy-1]?0:1)),make_pair(tx,ty+(a[tx][ty+1]?0:1))),tm+1));
	q.push(make_pair(make_pair(make_pair(sx,sy+(a[sx][sy+1]?0:1)),make_pair(tx,ty-(a[tx][ty-1]?0:1))),tm+1));
      }
    }
    if(q.empty()||tm>100)
      cout<<"NA"<<endl;
    else
      cout<<tm<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(x,b) ((x)>=0&&(x)<(b))
#define Y first
#define X second
#define INF 1 << 30

typedef pair<int, int> pii;
typedef queue<int> qi;
typedef pair<int, int> pi;

//int board[51][51], Y, X;
int board[500][500], Y, X;
int dx[] = {0,1,0,-1}, dy[] = {1,0,-1,0};

pii nextp1(pii now, int i){
  pii next = now;
  next.Y += dy[i];
  next.X += dx[i];
  //  printf("%d %d\n", next.Y, next.X);
  //  if(BW(next.Y, Y)) puts("YES");
  //  if(BW(next.X, X)) puts("YES");
  //  printf("%d\n", board[next.Y][next.X]);
  if(BW((next.Y), Y) && BW((next.X), X) && board[next.Y][next.X] == 0) return next;
  return now;
}

pii nextp2(pii now, int i){
  pii next = now;
  next.Y += -1 * dy[i];
  next.X += -1 * dx[i];
  if(BW(next.Y, Y) && BW(next.X, X) && board[next.Y][next.X] == 0) return next;
  return now;
}
     
int main(){
  while(scanf("%d%d", &X,  &Y) && X+Y){
    int tx, ty, kx, ky;
    scanf("%d%d%d%d", &tx, &ty, &kx, &ky); tx--; ty--; kx--; ky--;
    rep(i, Y) rep(j, X) scanf("%d", &board[i][j]); 

    //    rep(i, X){
    //      rep(j, Y) printf("%d ", board[i][j]); puts("");
    //    }

    map<pair<pi,pi>, bool>m; // t k
    queue<pii>tq, kq;
    tq.push(make_pair(ty, tx));
    kq.push(make_pair(ky, kx));
    m[make_pair(make_pair(ty, tx), make_pair(ky, kx))] = 1;
    int res = 1;
    while(!tq.empty()){
      if(res >= 100) break;
      //      printf("%d\n", res);
      int qsize = (int)tq.size();
      //      printf(" %d\n", qsize);
      rep(i, qsize){
	pii t = tq.front(); tq.pop();
	pii k = kq.front(); kq.pop();
	//	printf("%d %d %d %d\n", t.Y, t.X, k.Y, k.X);
	rep(j, 4){
	  pii nt = nextp1(t, j), nk = nextp2(k, j);
	  //	  printf("%d %d %d %d\n", t.Y, t.X, k.Y, k.X);
	  //	  printf("%d %d %d %d\n", nt.Y, nt.X, nk.Y, nk.X);

	  //	  if(f[nt.Y][nt.X][nk.Y][nk.X]) continue;
	  pair<pi, pi> next = make_pair(make_pair(nt.Y, nt.X), make_pair(nk.Y, nk.X));
	  if(m.count(next) == 1) continue;
	  if(nt == nk){
	    printf("%d\n", res);
	    //	    printf("%d %d %d %d\n", nt.Y, nt.X, nk.Y, nk.X);
	    goto end;
	  }
	  //	  fprintf(stderr, "%d %d %d %d\n", nt.Y, nt.X, nk.Y, nk.X);
	  //	  f[nt.Y][nt.X][nk.Y][nk.X] = 1;
	  m[next] = 1;
	  tq.push(nt); kq.push(nk);
	}
      }
      //      if(res > 100) break;
      res++;

    }
    //    printf("%d\n", (int)tq.size());
    puts("NA");
  end:;
  }
  return 0;
}