1. 

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, WS10/11
* Problem: 10189 - Minesweeper
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=13&page=show_problem&problem=1130
*
* @author Manuel Hager
* @version 1.0, 10/11/2010
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 0.532
*/

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class Main
{
private static List<char[][]> fields = new ArrayList<char[][]>();

private static int[] combination = new int[] { -1, 0, 1 };

private static void run(BufferedReader reader) throws Exception
{
while(true)
{
String line = reader.readLine();

String[] parts = line.split(" ");

int rows = Integer.parseInt(parts[0]);
int cols = Integer.parseInt(parts[1]);

if(rows == 0 && cols == 0) {
return;
}

char[][] field = new char[rows][cols];
for(int currRow = 0; currRow < rows; currRow++) {
line = reader.readLine();
for(int currCol = 0; currCol < cols; currCol++) {
field[currRow][currCol] = line.charAt(currCol);
}
}

fields.add(field);
}
}

private static void calc()
{
for(char[][] currField : fields)
{
for(int currRow = 0; currRow < currField.length; currRow++) {
for(int currCol = 0; currCol < currField[currRow].length; currCol++){
if(currField[currRow][currCol] == '.') {
currField[currRow][currCol] = getNumber(currField, currRow, currCol);
}
}
}
}
}

private static char getNumber(char[][] field, int row, int col)
{
int count = 0;

for(int r : combination)
{
for(int c: combination)
{
if(isOkay(field, row + r, col + c))
{
if(field[row + r][col + c] == '*') {
count++;
}
}
}
}

return Character.forDigit(count, 10);
}

private static boolean isOkay(char[][] field, int row, int col)
{
if(row < 0 || col < 0)
return false;

if(row >= field.length)
return false;

if(col >= field[0].length)
return false;

return true;
}

public static void main(String[] args)
{
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

run(reader);

reader.close();
}
catch(Exception ex)
{}

calc();

//******************************
//Ausgabe!
int count = 1;
for(char[][] currField : fields)
{
System.out.printf("Field #%d:%n", count++);

for(int currRow = 0; currRow < currField.length; currRow++) {
for(int currCol = 0; currCol < currField[currRow].length; currCol++){
System.out.print(currField[currRow][currCol]);
}
System.out.println();
}

//Ende? kein lineFeed notwendig.
if(fields.size() + 1 != count) {
System.out.println();
}
}
}
}