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 < 100 {
				//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++: # define _CRT_SECUREj_NO_WARNINGS 1
# define _USE_MATH_DEFINES
# include <iostream>
# include <numeric>
# include <string>
# include <bitset>
# include <vector>
# include <algorithm>
# include <cstdlib>
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iomanip>
# include <queue>
# include <sstream>
# include <climits>
# include <cmath>
# include <list>
# include <functional>
# include <string>
# include <ctime>
# include <set>
# include <forward_list>
# include <map>
# include <stack>
using namespace std;
# define INF ((int)(1<<25))
# define REP(i,n) for(int i=0;i<(int)n;i++)
# define FOR(i,n) REP(i,n)
# define FORI(i,k,n) for(int i=k;i<(int)n;i++)
# define TORAD 2.0*M_PI/360.0
# define INT(x) int x;cin>>x;
# define ALL(x) (x).begin(),(x).end()
# define RALL(x) (x).rbegin(),(x).rend()
# define DEBUG(x) cout<<#x<<":"<<x<<endl;
# define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = { 0,1,0,-1 }, dy[4] = { -1,0,1,0 };
template<class T> void debug(T a) { for (auto i : a)cout << i << endl; }

const int M = 60;

int main()
{
	int x, y;
	while (cin >> x >> y&&x)
	{
		int a[M][M] = {};
		int ans[M][M][M][M];
		memset(ans, -1, sizeof ans);
		pii t, k;
		cin >> t.first >> t.second;
		cin >> k.first >> k.second;
		FORI(i,1,y+1)FORI(j, 1, x + 1)cin >> a[j][i];

		struct P
		{
			pii t, k;
			int c;
		};
		bool flag = false;
		queue<P> que; que.push(P{ t,k,0 });
		ans[t.first][t.second][k.first][k.second] = 0;
		while (!que.empty())
		{
			P now = que.front(); que.pop();
			if (now.c >= 100)continue;
			if (now.t == now.k)
			{
				flag = true;
				cout << now.c << endl;
				break;
			}

			FOR(i, 4)
			{
				auto move = [&a,&x,&y](pii p,int ddx,int ddy)->pii 
				{
					pii tt = p; tt.first += ddx; tt.second += ddy;
					if (a[tt.first][tt.second]||tt.first<1||tt.second<1||tt.first>x||tt.second>y)return p;
					return tt;
				};
				P next = now;
				next.t = move(next.t, dx[i], dy[i]);
				next.k = move(next.k, -1 * dx[i], -1 * dy[i]);
				next.c++;

				if (ans[next.t.first][next.t.second][next.k.first][next.k.second] == -1)
				{
					ans[next.t.first][next.t.second][next.k.first][next.k.second] = next.c;
					que.push(next);
				}
			}

		}

		if (!flag)cout << "NA" << endl;
	}
}

C++: # include <iostream>
# include <queue>
# define MAX_H 50
# define MAX_W 50

typedef std::pair<int, int> P;

int w, h;
int tx, ty, kx, ky;
int board[MAX_H][MAX_W];

int dtx[4] = { 1,-1,0,0 }, dty[4] = { 0,0,1,-1 };
int dkx[4] = { -1,1,0,0 }, dky[4] = { 0,0 - 1,1 };

struct Points {
	P t, k;
	int c;
	Points( P _t, P _k, int _c) : t(_t), k(_k), c(_c) {}
};

int bfs() {
	std::queue<Points> que;
	que.push(Points(P(tx, ty), P(kx, ky), 0));
	while (!que.empty()) {
		P t = que.front().t, k = que.front().k;
		int c = que.front().c;
		que.pop();
		if (c >= 100)
			return -1;
		for (int i = 0; i < 4; ++i) {
			int ntx = t.first + dtx[i];
			int nty = t.second + dty[i];
			int nkx = k.first + dkx[i];
			int nky = k.second + dky[i];
			if (ntx == nkx&&nty == nky)
				return c;
			bool t_flag = (0 <= ntx && 0 <= nty&&ntx < w&&nty < h&&!board[nty][ntx]);
			bool k_flag = (0 <= nkx && 0 <= nky&&nkx < w&&nky < h&&!board[nky][nkx]);
			if (t_flag&&k_flag)
				que.push(Points(P(ntx, nty), P(nkx, nky), c + 1));
			else if (!t_flag&&k_flag)
				que.push(Points(t, P(nkx, nky), c + 1));
			else if (t_flag&&!k_flag)
				que.push(Points(P(ntx, nty), k, c + 1));
			else
				continue;
		}
	}
	return -1;
}

int main() {
	while (1) {
		std::cin >> w >> h;
		if (w == 0 && h == 0)
			break;
		std::cin >> tx >> ty >> kx >> ky;
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				std::cin >> board[i][j];
			}
		}
		if (bfs() == -1)
			std::cout << "NA" << std::endl;
		else
			std::cout << bfs() << std::endl;
	}
	return 0;
}