#include <iostream>

using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	unsigned short n, m;
	char mat[10][10];
	cin >> n >> m;

	for (unsigned short i = 0; i < n; ++i)
		for (unsigned short j = 0; j < m; ++j)
			cin >> mat[i][j];

	for(unsigned short i = 0; i < n; ++i)
		for (unsigned short j = 0; j < m; ++j)
		{
			if (mat[i][j] - '0' == 0)
			{
				if (!i && !j)
				{
					if (mat[i][j + 1] == '.') mat[i][j + 1] = 'n';
					if (mat[i + 1][j] == '.')mat[i + 1][j] = 'n';
				}
				else

				if (!i)
				{
					if (mat[i][j + 1] == '.') mat[i][j + 1] = 'n';
					if (mat[i][j - 1] == '.')mat[i][j - 1] = 'n';
					if (mat[i + 1][j] == '.') mat[i + 1][j] = 'n';
				}
				else

				if (!j)
				{
						if (mat[i][j + 1] == '.') mat[i][j + 1] = 'n';
						if (mat[i - 1][j] == '.')mat[i - 1][j] = 'n';
						if (mat[i + 1][j] == '.') mat[i + 1][j] = 'n';
				}
				else
				{
					if (mat[i][j + 1] == '.') mat[i][j + 1] = 'n';
					if (mat[i - 1][j] == '.')mat[i - 1][j] = 'n';
					if (mat[i + 1][j] == '.') mat[i + 1][j] = 'n';
					if (mat[i][j - 1] == '.') mat[i][j - 1] = 'n';
				}

			}
		}

	bool tacka = false;
	for(unsigned short i = 0; i < n; ++i)
		for (unsigned short j = 0; j < m; ++j)
		{
			if (mat[i][j] == '.')
			{
				tacka = true;
				break; break;
			}
		}

	if (!tacka) cout << -1 <<endl;



	return 0;
}