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 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++
			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<iostream>
#include<utility>
#include<queue>
using namespace std;
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
int main() {
	int W, H, tx, ty, kx, ky;
	int d[52][52];
	bool memo[52][52][52][52];
	while (true) {
		cin >> W >> H;
		if (W == 0)break;
		for (int i = 0; i < 52; i++) {
			for (int j = 0; j < 52; j++) {
				for (int k = 0; k < 52; k++) {
					for (int l = 0; l < 52; l++) {
						memo[i][j][k][l] = false;
					}
				}
			}
		}
		for (int i = 0; i < 52; i++) {
			for (int j = 0; j < 52; j++) {
				d[i][j] = 1;
			}
		}
		cin >> tx >> ty >> kx >> ky;
		for (int i = 1; i < H + 1; i++) {
			for (int j = 1; j < W + 1; j++) {
				cin >> d[j][i];
			}
		}
		pair<pair<int, int>, pair<int, int> >num;
		num.first.first = tx;
		num.first.second = ty;
		num.second.first = kx;
		num.second.second = ky;
		queue<pair<pair<int, int>, pair<int, int> > > q;
		q.push(num);
		int qsize = 1;
		int loopcount = 0;
		while (!q.empty()) {
			num = q.front();
			q.pop();
			if (qsize == 0) {
				qsize = q.size();
				loopcount++;
				if (loopcount == 100 || q.size()==0) {
					cout << "NA" << endl;
					break;
				}
			}
			qsize--;
			if (memo[num.first.first][num.first.second][num.second.first][num.second.second]) {
				continue;
			}
			else {
				memo[num.first.first][num.first.second][num.second.first][num.second.second] = true;
			}
			if (num.first.first == num.second.first && num.first.second == num.second.second) {
				cout << loopcount << endl;
				break;
			}
			for (int i = 0; i < 4; i++) {
				pair<pair<int, int>, pair<int, int> >copy;
				copy = num;
				copy.first.first += dx[i];
				copy.first.second += dy[i];
				if (d[copy.first.first][copy.first.second]) {
					copy.first.first -= dx[i];
					copy.first.second -= dy[i];
				}
				copy.second.first -=dx[i];
				copy.second.second -= dy[i];
				if (d[copy.second.first][copy.second.second]) {
					copy.second.first += dx[i];
					copy.second.second += dy[i];
				}
				q.push(copy);
			}
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <cstring>
#include <cstdlib>

using namespace std;

char field[51][51];

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

int w,h;
int bfs(int stx,int sty,int skx,int sky){

	queue<pair<pair<int,int>,pair<int,int> > > *prv=new queue<pair<pair<int,int>,pair<int,int> > >();
	queue<pair<pair<int,int>,pair<int,int> > > *nxt=new queue<pair<pair<int,int>,pair<int,int> > >();
	prv->push(make_pair(make_pair(sty,stx),make_pair(sky,skx)));
	int cnt=1;
	bool passed[51][51][51][51];
	memset(passed,0,sizeof(passed));
	while(prv->size()){
		if(cnt>100)
			return -1;
		while(prv->size()){
			pair<pair<int,int>,pair<int,int> > ppp = prv->front();
			prv->pop();
			pair<int,int> tp=ppp.first;
			pair<int,int> kp=ppp.second;
			if(passed[tp.first][tp.second][kp.first][kp.second]){
				continue;
			}
			passed[tp.first][tp.second][kp.first][kp.second]=true;
			for(int i = 0; i < 4; i++){
				int nky=kp.first+dy[i];
				int nkx=kp.second+dx[i];
				int nty=tp.first-dy[i];
				int ntx=tp.second-dx[i];
				// ©¸ä«ªÍÍOÉoÈ¢ê
				if(nky>=0&&nkx>=0&&nkx<w&&nky<h&&field[nky][nkx]=='0'){
					// ½©ä«ªÍÍOÉoéê
					if(!(nty>=0&&ntx>=0&&ntx<w&&nty<h&&field[nty][ntx]=='0')){
						nxt->push(make_pair(tp,make_pair(nky,nkx)));
						if(tp==make_pair(nky,nkx))
							return cnt;
					}
					else{
						nxt->push(make_pair(make_pair(nty,ntx),make_pair(nky,nkx)));
						if(make_pair(nty,ntx)==make_pair(nky,nkx))
							return cnt;
					}
				}
				// ½©ä«ªÍÍOÉoÈ¢ê
				else if(nty>=0&&ntx>=0&&ntx<w&&nty<h&&field[nty][ntx]=='0'){
					// ©¸ä«ªÍÍOÉoéê
					if(!(nky>=0&&nkx>=0&&nkx<w&&nky<h&&field[nky][nkx]=='0')){
						nxt->push(make_pair(make_pair(nty,ntx),kp));
						if(kp==make_pair(nty,ntx))
							return cnt;
					}
					else{
						nxt->push(make_pair(make_pair(nty,ntx),make_pair(nky,nkx)));
						if(make_pair(nty,ntx)==make_pair(nky,nkx))
							return cnt;
					}
				}
			}
		}
		cnt++;
		swap(prv,nxt);
	}
	return -1;
}

int main(){

	while(cin>>w>>h&&!(w==0&&h==0)){
		int ty,tx,kx,ky;
		cin>>tx>>ty>>kx>>ky;
		ty--;
		tx--;
		kx--;
		ky--;
		for(int i = 0; i < h; i++){
			for(int j= 0; j < w; j++){
				cin>>field[i][j];
			}
		}

		int ret=bfs(tx,ty,kx,ky);
		if(ret==-1)
			cout<<"NA"<<endl;
		else
			cout<<ret<<endl;
	}

	return 0;
}