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 <vector>
#include <queue>
#include <string.h>

using namespace std;
static const int dx[] = {1, 0, -1, 0};
static const int dy[] = {0, -1, 0, 1};
bool isVisited[51][51][51][51];

class point
{
public:
	int tx, ty;
	int kx, ky;
};

class data
{
public:
	point p;
	int cnt;
};

void solve()
{
	int X, Y;
	while (cin >> X >> Y, X || Y)
	{
		data d;
		cin >> d.p.tx >> d.p.ty >> d.p.kx >> d.p.ky;
		--d.p.tx; --d.p.ty;
		--d.p.kx; --d.p.ky;
		d.cnt = 0;
		vector< vector<char> > field(Y, vector<char>(X));
		for (int i = 0; i < Y; ++i)
		{
			for (int j = 0; j < X; ++j)
			{
				cin >> field[i][j];
			}
		}
		memset(isVisited, 0, sizeof(isVisited));
		queue<data> Que;
		Que.push(d);
		isVisited[d.p.ty][d.p.tx][d.p.ky][d.p.kx] = true;
		bool findFlag = false;
		while (!Que.empty())
		{
			data cur = Que.front();
			Que.pop();
			if (cur.cnt >= 100)
			{
				break;
			}
			if (cur.p.tx == cur.p.kx && cur.p.ty == cur.p.ky)
			{
				cout << cur.cnt << endl;
				findFlag = true;
				break;
			}
			for (int i = 0; i < 4; ++i)
			{
				data next;
				next.p.tx = cur.p.tx + dx[i];
				next.p.ty = cur.p.ty + dy[i];
				if (!(0 <= next.p.tx && next.p.tx < X && 0 <= next.p.ty && next.p.ty < Y) || field[next.p.ty][next.p.tx] == '1')
				{
					next.p.tx = cur.p.tx;
					next.p.ty = cur.p.ty;
				}
				next.p.kx = cur.p.kx + dx[(i + 2) % 4];
				next.p.ky = cur.p.ky + dy[(i + 2) % 4];
				if (!(0 <= next.p.kx && next.p.kx < X && 0 <= next.p.ky && next.p.ky < Y) || field[next.p.ky][next.p.kx] == '1')
				{
					next.p.kx = cur.p.kx;
					next.p.ky = cur.p.ky;
				}
				next.cnt = cur.cnt + 1;
				if (isVisited[next.p.ty][next.p.tx][next.p.ky][next.p.kx] == false)
				{
					Que.push(next);
					isVisited[next.p.ty][next.p.tx][next.p.ky][next.p.kx] = true;
				}
			}
		}
		if (!findFlag)
		{
			cout << "NA" << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include <iostream>
#include <algorithm>
#include <queue>
#include <utility>
#include <map>
using namespace std;

const int Y = 50, X = 50, guard = -1;
typedef pair<int, int> P;


int bfs(const int (*board)[Y + 2], P t, P k){
	int dx[] = {0, -1, 1, 0}, dy[] = {-1, 0, 0, 1};
	// pair<P, P> -> first = t, second = k
	queue<pair<P, P> > q;
	map<pair<P, P>, int> visited;
	q.push(make_pair(t, k));
	visited[make_pair(t, k)] = 0;
	while(!q.empty()){
		pair<P, P> picked = q.front();
		q.pop();
		if(visited[picked] >= 100) break;
		P ct = picked.first, ck = picked.second;
		if(ct == ck) return visited[picked];
		for(int i = 0;i < 4;++i){
			pair<P, P> nextp;
			P nextt, nextk;
			int tdx, tdy, kdx, kdy;
			tdx = dx[i]; tdy = dy[i];
			kdx = -tdx; kdy = -tdy;
			nextt = board[ct.first + tdx][ct.second + tdy] != guard ? P(ct.first + tdx, ct.second + tdy) : ct;
			nextk = board[ck.first + kdx][ck.second + kdy] != guard ? P(ck.first + kdx, ck.second + kdy) : ck;
			nextp = make_pair(nextt, nextk);
			if(visited[nextp] == 0){
				q.push(nextp);
				visited[nextp] = visited[picked] + 1;
			}
		}
	}
	return -1;
}

int main(){
	int board[X + 2][Y + 2];
	int x, y, a, res;
	P t, k;
	while(cin >> x >> y, x && y){
		for(int i = 0;i <= X + 1;++i){
			fill(board[i], board[i] + (Y + 2), -1);
		}
		cin >> t.first >> t.second;
		cin >> k.first >> k.second;
		for(int i = 1;i <= y;++i){
			for(int j = 1;j <= x;++j){
				cin >> a;
				board[i][j] = -a;
			}
		}
		res = bfs(board, t, k);
		if(res != guard) cout << res << endl;
		else cout << "NA" << endl;
	}
	return 0;
}

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

bool d[50][50][50][50];
int X, Y, tx, ty, kx, ky;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0}, mem[50][50];
int main()
{
loop:
  cin >> X >> Y;
  if(!X)
    return 0;
  cin >> tx >> ty >> kx >> ky;
  tx--; ty--; kx--; ky--;
  for(int y = 0; y < Y; y++)
  {
    for(int x = 0; x < X; x++)
    {
      int a;
      cin >> a;
      mem[y][x] = a;
    }
  }

  fill_n((bool*)d, 50 * 50 * 50 * 50, false);
  
  queue<int> q;
  q.push(tx);
  q.push(ty);
  q.push(kx);
  q.push(ky);
  q.push(0);
  while(!q.empty())
  {
    int x1 = q.front(); q.pop();
    int y1 = q.front(); q.pop();
    int x2 = q.front(); q.pop();
    int y2 = q.front(); q.pop();
    int n = q.front(); q.pop();
    
    if(n == 100)
    {
      cout << "NA" << endl;
      break;
    }
    
    if(y1 == y2 && x1 == x2)
    {
      cout << n << endl;
      goto loop;
    }

    for(int i = 0; i < 4; i++)
    {
      int tx1 = x1 + dx[i];
      int ty1 = y1 + dy[i];
      int tx2 = x2 - dx[i];
      int ty2 = y2 - dy[i];

      bool b1 = ty1 < 0 || ty1 >= Y || tx1 < 0 || tx1 >= X || mem[ty1][tx1] == 1;
      bool b2 = ty2 < 0 || ty2 >= Y || tx2 < 0 || tx2 >= X || mem[ty2][tx2] == 1;

      if(b1)
      {
        tx1 = x1;
        ty1 = y1;
      }
      if(b2)
      {
        tx2 = x2;
        ty2 = y2;
      }
      if(d[tx1][ty1][tx2][ty2])
        continue; 

      d[tx1][ty1][tx2][ty2] = true;
      q.push(tx1);
      q.push(ty1);
      q.push(tx2);
      q.push(ty2);
      q.push(n + 1);
    }
  }
  cout << "NA" << endl;
  goto loop;
}