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<queue>
using namespace std;

const int INF = 100000000;

class Point{
public:
  int x;
  int y;
  int x2;
  int y2;
};

int main(){
  while(true){
    int N,M;
    cin >> N >> M;

    if(N==0&&M==0) break;

    int F[102][102];
    for(int y=0; y<=N+1; y++){
      for(int x=0; x<=M+1; x++){
        F[y][x] = 1;
      }
    }

    int tx,ty,kx,ky;
    cin >> tx >> ty >> kx >> ky;

    for(int y=1; y<=N; y++){
      for(int x=1; x<=M; x++){
        cin >> F[y][x];
      }
    }

    int D[102][102];
    for(int y=1; y<=N; y++){
      for(int x=1; x<=M; x++){
        D[y][x] = INF;
      }
    }

    queue<Point> que;

    Point p;
    p.x = tx; p.y = ty;
    p.x2 = kx; p.y2 = ky;
    que.push(p);
    D[p.y][p.x] = 0;

    while(!que.empty()){
      p = que.front(); que.pop();
      cout << "xy " << p.x << ' ' << p.y << ' ' << p.x2 << ' ' << p.y2 << endl;
      cout << D[p.y][p.x] << endl;

      if(p.x==p.x2 && p.y==p.y2){
        cout << D[p.y][p.x] << endl;
        break;
      }

      if(F[p.y-1][p.x]!= 1 && F[p.y2+1][p.x2]!= 1 && D[p.y-1][p.x]==INF){
        Point q;
        q.x = p.x; q.y = p.y-1;
        q.x2 = p.x2; q.y2 = p.y2+1;

      /*  if(F[q.x][q.y]==1){
          q.y += 1;
        }else if(F[q.x2][q.y2]==1){
          q.y2 = p.y2;
        }*/

        que.push(q);
        D[q.y][q.x] = D[p.y][p.x]+1;
      }
      if(F[p.y+1][p.x]!=1 && F[p.y2-1][p.x2]!=1 && D[p.y+1][p.x]==INF){
        Point q;
        q.x = p.x; q.y = p.y+1;
        q.x2 = p.x2; q.y2 = p.y2-1;

        /*if(F[q.x][q.y]==1){
          q.y = p.y;
        }else if(F[q.x2][q.y2]==1){
          q.y2 = p.y2;
        }*/

        que.push(q);
        D[q.y][q.x] = D[p.y][p.x]+1;
      }
      if(F[p.y][p.x-1]!=1 && F[p.y2][p.x2+1]!=1 && D[p.y][p.x-1]==INF){
        Point q;
        q.x = p.x-1; q.y = p.y;
        q.x2 = p.x2+1; q.y2 = p.y2;

    /*    if(F[q.x][q.y]==1){
          q.x = p.x;
        }else if(F[q.x2][q.y2]==1){
          q.x2 = p.x2;
        }*/

        que.push(q);
        D[q.y][q.x] = D[p.y][p.x]+1;
      }
      if(F[p.y][p.x+1]!=1 && F[p.y2][p.x2-1]!=1 && D[p.y][p.x+1]==INF){
        Point q;
        q.x = p.x+1; q.y = p.y;
        q.x2 = p.x2-1; q.y2 = p.y2;

      /*  if(F[q.x][q.y]==1){
          q.x = p.x;
        }else if(F[q.x2][q.y2]==1){
          q.x2 = p.x2;
        }*/

        que.push(q);
        D[q.y][q.x] = D[p.y][p.x]+1;
      }

    }
    if(p.x!=p.x2)
      cout << "NA" << endl;
  }
  return 0;
}

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

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

bool f[50][50];
bool h[50][50][50][50];

class State
{
public:
	int lx,ly,rx,ry,c;
	State(int lx, int ly, int rx, int ry, int c)
	:lx(lx),ly(ly),rx(rx),ry(ry),c(c)
	{}
};
int main()
{
	int W,H;
	while(cin >> W >> H, (W||H))
	{
		memset(h,0,sizeof(h));

		int lx,ly,rx,ry;
		cin >> lx >> ly >> rx >> ry;

		lx--; ly--;
		rx--; ry--;

		for(int i=0; i<H; i++)
		for(int j=0; j<W; j++)
			cin >> f[j][i];

		queue<State> q;
		q.push(State(lx,ly,rx,ry,0));

		bool g=false;
		while(!q.empty())
		{
			State s = q.front(); q.pop();
			if(s.c >= 100) break;

			if(s.lx==s.rx&&s.ly==s.ry)
			{
				g=true;
				cout << s.c << endl;
				break;
			}

			for(int i=0; i<4; i++)
			{
				int tlx=s.lx+dx[i], tly=s.ly+dy[i];
				int trx=s.rx+dx[(i+2)%4], ttry=s.ry+dy[(i+2)%4];

				if(tlx<0||tlx>=W||tly<0||tly>=H||f[tlx][tly])
				{
					tlx = s.lx;
					tly = s.ly;
				}
				if(trx<0||trx>=W||ttry<0||ttry>=H||f[trx][ttry])
				{
					trx = s.rx;
					ttry = s.ry;
				}

				if(h[tlx][tly][trx][ttry]) continue;
				h[tlx][tly][trx][ttry]=1;
				
				q.push(State(tlx,tly,trx,ttry,s.c+1));

			}
		}

		if(!g) cout << "NA" << endl;

	}
}