#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
bool f, vis[105][105][10];
int m, n, a[105][105];
int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0}, dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
struct node {
int x, y, dis, d;
};
void spfa() {
queue<node> q;
node tmp;
tmp.x = tmp.y = 1;
tmp.dis = 0;
tmp.d = 8;
q.push(tmp);
while (!q.empty()) {
node tmp = q.front();
int x = tmp.x, y = tmp.y;
//cout << x << ' ' << y << ' ' << a[x][y] << ' ' << tmp.d << ' ' << tmp.dis << '\n';
q.pop();
//cout << x << ' ' << y << ' ' << dis[x][y] << '\n';
if (x == m && y == n) {
printf("%d", tmp.dis);
f = 1;
return ;
}
for (int i = 0; i < 8; i++) {
if (i == tmp.d)
continue;
int tx = x + dx[i] * a[x][y], ty = y + dy[i] * a[x][y];
if (0 < tx && tx <= m && 0 < ty && ty <= n && !vis[tx][ty][tmp.d]) {
vis[tx][ty][tmp.d] = 1;
node t;
t.x = tx;
t.y = ty;
t.dis = tmp.dis + 1;
t.d = i;
q.push(t);
}
}
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
spfa();
if (!f)
printf("NEVER");
return 0;
}
求助!