#include <iostream>
#include <stack>
#include <utility>
#define down 10
#define left -1
#define right 1
#define up -10
using namespace std;
void print(int n, int m, int** matrix)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (matrix[i][j] == 0 || matrix[i][j] == 1)
cout << matrix[i][j] << " ";
else
cout << (char)matrix[i][j] << " ";
}
cout << endl;
}
}
bool search(int n, int m, int** matrix, stack<pair<int,int>>& route, int dir)
{
pair<int, int> npos = make_pair(dir / 10 + route.top().first, dir % 10 + route.top().second);
if (npos.first < 0 || npos.first >= n || npos.second < 0 || npos.second >= m || matrix[npos.first][npos.second] != 0)
return false;
else
{
route.push(npos);
matrix[npos.first][npos.second] = 1;
if (npos.first == n - 1)
return true;
else
{
int* priorityDirs;
switch (dir)
{
case down:
priorityDirs = new int[3]{left, down, right};
break;
case left:
priorityDirs = new int[3]{left, down, up};
break;
case right:
priorityDirs = new int[3]{down, right, up};
break;
case up:
priorityDirs = new int[3]{left, right, up};
break;
}
bool found = true;
for (int i = 0; i < 3; i++)
{
found = search(n, m, matrix, route, priorityDirs[i]);
if (found)
return true;
}
matrix[npos.first][npos.second] = 0;
route.pop();
return false;
}
}
}
stack<pair<int,int>> findRoute(int n, int m, int** matrix, int begin)
{
stack<pair<int,int>> route;
route.push(make_pair(0, begin));
if (n != 1 && !search(n, m, matrix, route, down))
throw -1;
return route;
}
int main()
{
ios::sync_with_stdio(false);
int n, m;
scanf("%d%d", &n, &m);
int** matrix = new int*[n];
for (int i = 0; i < n; i++)
matrix[i] = new int[m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
scanf("%d", &matrix[i][j]);
bool possible = true;
int symbol = -1;
for (int i = 0; i < m; i++)
{
if (matrix[0][i] == 0)
{
symbol++;
try
{
stack<pair<int,int>> route = findRoute(n, m, matrix, i);
while (!route.empty())
{
pair<int, int> pos = route.top();
matrix[pos.first][pos.second] = 'a' + symbol;
route.pop();
}
}
catch (...)
{
possible = false;
break;
}
}
}
cout << (possible ? "YES" :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