import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        CTheLabyrinth solver = new CTheLabyrinth();
        solver.solve(1, in, out);
        out.close();
    }

    static class CTheLabyrinth {
        private int n;
        private int m;

        private int index(int i, int j) {
            return i * m + j;
        }

        private boolean isValid(int x, int y) {
            return x >= 0 && x < n && y >= 0 && y < m;
        }

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            n = in.nextInt();
            m = in.nextInt();
            char[][] arr = new char[n][m];
            CTheLabyrinth.DisjointSet ds = new CTheLabyrinth.DisjointSet(n * m);
            for (int i = 0; i < n; i++) {
                String str = in.nextLine();
                for (int j = 0; j < m; j++) arr[i][j] = str.charAt(j);
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (arr[i][j] == '.') {
                        if (isValid(i - 1, j) && arr[i - 1][j] == '.') ds.union(index(i, j), index(i - 1, j));
                        if (isValid(i, j + 1) && arr[i][j + 1] == '.') ds.union(index(i, j), index(i, j + 1));
                        if (isValid(i + 1, j) && arr[i + 1][j] == '.') ds.union(index(i, j), index(i + 1, j));
                        if (isValid(i, j - 1) && arr[i][j - 1] == '.') ds.union(index(i, j), index(i, j - 1));
                    }
                }
            }
            HashSet<Integer> hs = new HashSet<>();
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (arr[i][j] == '.') out.print('.');
                    else {
                        hs.clear();
                        if (isValid(i - 1, j) && arr[i - 1][j] == '.') hs.add(ds.root(index(i - 1, j)));
                        if (isValid(i, j + 1) && arr[i][j + 1] == '.') hs.add(ds.root(index(i, j + 1)));
                        if (isValid(i + 1, j) && arr[i + 1][j] == '.') hs.add(ds.root(index(i + 1, j)));
                        if (isValid(i, j - 1) && arr[i][j - 1] == '.') hs.add(ds.root(index(i, j - 1)));
                        int res = 0;
                        for (int x : hs) res += ds.setSize[x];
                        res++;
                        res = Util.mod(res, 10);
                        out.print(res);
                    }

                }
                out.println();
            }
            out.flush();
        }

        static class DisjointSet {
            private int[] parent;
            private int[] setSize;
            private int numOfSets;

            private DisjointSet(int n) {
                parent = new int[n];
                setSize = new int[n];
                for (int i = 0; i < n; i++) {
                    setSize[i] = 1;
                    parent[i] = i;
                }
                numOfSets = n;
            }

            private int root(int x) {
                if (x == parent[x]) return x;
                return parent[x] = root(parent[x]);
            }

            private boolean _union(int n1, int n2) {
                if (n1 == n2) return false;
                if (setSize[n1] > setSize[n2]) {
                    parent[n2] = parent[n1];
                    setSize[n1] += setSize[n2];
                } else {
                    parent[n1] = parent[n2];
                    setSize[n2] += setSize[n1];
                }
                numOfSets--;
                return true;
            }

            private boolean union(int n1, int n2) {
                return _union(root(n1), root(n2));
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[8192];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public InputReader(FileInputStream file) {
            this.stream = file;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res = (res << 3) + (res << 1) + c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

    }

    static class Util {
        public static int mod(int a, int mod) {
            int res = a % mod;
            return res >= 0 ? res : res + mod;
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;
        private ArrayList<String> res = new ArrayList<>();
        private StringBuilder sb = new StringBuilder("");

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void println() {
            res.add(sb.toString());
            sb = new StringBuilder("");
//        writer.println();
//        writer.flush();
        }

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                sb.append(objects[i]);
            }
//        writer.println();
//        writer.flush();
        }

        public void close() {
            flush();
            writer.close();
        }

        public void flush() {
            for (String str : res) writer.printf("%s\n", str);
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        CTheLabyrinth solver = new CTheLabyrinth();
        solver.solve(1, in, out);
        out.close();
    }

    static class CTheLabyrinth {
        private int n;
        private int m;

        private int index(int i, int j) {
            return i * m + j;
        }

        private boolean isValid(int x, int y) {
            return x >= 0 && x < n && y >= 0 && y < m;
        }

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            n = in.nextInt();
            m = in.nextInt();
            char[][] arr = new char[n][m];
            CTheLabyrinth.DisjointSet ds = new CTheLabyrinth.DisjointSet(n * m);
            for (int i = 0; i < n; i++) {
                String str = in.nextLine();
                for (int j = 0; j < m; j++) arr[i][j] = str.charAt(j);
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (arr[i][j] == '.') {
                        if (isValid(i - 1, j) && arr[i - 1][j] == '.') ds.union(index(i, j), index(i - 1, j));
                        if (isValid(i, j + 1) && arr[i][j + 1] == '.') ds.union(index(i, j), index(i, j + 1));
                        if (isValid(i + 1, j) && arr[i + 1][j] == '.') ds.union(index(i, j), index(i + 1, j));
                        if (isValid(i, j - 1) && arr[i][j - 1] == '.') ds.union(index(i, j), index(i, j - 1));
                    }
                }
            }
            HashSet<Integer> hs = new HashSet<>();
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (arr[i][j] == '.') out.print('.');
                    else {
                        hs.clear();
                        if (isValid(i - 1, j) && arr[i - 1][j] == '.') hs.add(ds.root(index(i - 1, j)));
                        if (isValid(i, j + 1) && arr[i][j + 1] == '.') hs.add(ds.root(index(i, j + 1)));
                        if (isValid(i + 1, j) && arr[i + 1][j] == '.') hs.add(ds.root(index(i + 1, j)));
                        if (isValid(i, j - 1) && arr[i][j - 1] == '.') hs.add(ds.root(index(i, j - 1)));
                        int res = 0;
                        for (int x : hs) res += ds.setSize[x];
                        res++;
                        res = Util.mod(res, 10);
                        out.print(res);
                    }

                }
                out.println();
            }
            out.flush();
        }

        static class DisjointSet {
            private int[] parent;
            private int[] setSize;
            private int numOfSets;

            private DisjointSet(int n) {
                parent = new int[n];
                setSize = new int[n];
                for (int i = 0; i < n; i++) {
                    setSize[i] = 1;
                    parent[i] = i;
                }
                numOfSets = n;
            }

            private int root(int x) {
                if (x == parent[x]) return x;
                return parent[x] = root(parent[x]);
            }

            private boolean _union(int n1, int n2) {
                if (n1 == n2) return false;
                if (setSize[n1] > setSize[n2]) {
                    parent[n2] = parent[n1];
                    setSize[n1] += setSize[n2];
                } else {
                    parent[n1] = parent[n2];
                    setSize[n2] += setSize[n1];
                }
                numOfSets--;
                return true;
            }

            private boolean union(int n1, int n2) {
                return _union(root(n1), root(n2));
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[8192];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public InputReader(FileInputStream file) {
            this.stream = file;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res = (res << 3) + (res << 1) + c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

    }

    static class Util {
        public static int mod(int a, int mod) {
            int res = a % mod;
            return res >= 0 ? res : res + mod;
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;
        private ArrayList<String> res = new ArrayList<>();
        private StringBuilder sb = new StringBuilder("");

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void println() {
            res.add(sb.toString());
            sb = new StringBuilder("");
        }

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                sb.append(objects[i]);
            }
        }

        public void close() {
            writer.close();
        }

        public void flush() {
            for (String str : res) writer.printf("%s\n", str);
            res.clear();
            sb = new StringBuilder("");
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
//test
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        CTheLabyrinth solver = new CTheLabyrinth();
        solver.solve(1, in, out);
        out.close();
    }

    static class CTheLabyrinth {
        private int n;
        private int m;

        private int index(int i, int j) {
            return i * m + j;
        }

        private boolean isValid(int x, int y) {
            return x >= 0 && x < n && y >= 0 && y < m;
        }

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            n = in.nextInt();
            m = in.nextInt();
            char[][] arr = new char[n][m];
            CTheLabyrinth.DisjointSet ds = new CTheLabyrinth.DisjointSet(n * m);
            for (int i = 0; i < n; i++) {
                String str = in.nextLine();
                for (int j = 0; j < m; j++) arr[i][j] = str.charAt(j);
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (arr[i][j] == '.') {
                        if (isValid(i - 1, j) && arr[i - 1][j] == '.') ds.union(index(i, j), index(i - 1, j));
                        if (isValid(i, j + 1) && arr[i][j + 1] == '.') ds.union(index(i, j), index(i, j + 1));
                        if (isValid(i + 1, j) && arr[i + 1][j] == '.') ds.union(index(i, j), index(i + 1, j));
                        if (isValid(i, j - 1) && arr[i][j - 1] == '.') ds.union(index(i, j), index(i, j - 1));
                    }
                }
            }
            HashSet<Integer> hs = new HashSet<>();
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (arr[i][j] == '.') out.print('.');
                    else {
                        hs.clear();
                        if (isValid(i - 1, j) && arr[i - 1][j] == '.') hs.add(ds.root(index(i - 1, j)));
                        if (isValid(i, j + 1) && arr[i][j + 1] == '.') hs.add(ds.root(index(i, j + 1)));
                        if (isValid(i + 1, j) && arr[i + 1][j] == '.') hs.add(ds.root(index(i + 1, j)));
                        if (isValid(i, j - 1) && arr[i][j - 1] == '.') hs.add(ds.root(index(i, j - 1)));
                        int res = 0;
                        for (int x : hs) res += ds.setSize[x];
                        res++;
                        res = Util.mod(res, 10);
                        out.print(res);
                    }

                }
                out.println();
            }
            out.flush();
        }

        static class DisjointSet {
            private int[] parent;
            private int[] setSize;
            private int numOfSets;

            private DisjointSet(int n) {
                parent = new int[n];
                setSize = new int[n];
                for (int i = 0; i < n; i++) {
                    setSize[i] = 1;
                    parent[i] = i;
                }
                numOfSets = n;
            }

            private int root(int x) {
                if (x == parent[x]) return x;
                return parent[x] = root(parent[x]);
            }

            private boolean _union(int n1, int n2) {
                if (n1 == n2) return false;
                if (setSize[n1] > setSize[n2]) {
                    parent[n2] = parent[n1];
                    setSize[n1] += setSize[n2];
                } else {
                    parent[n1] = parent[n2];
                    setSize[n2] += setSize[n1];
                }
                numOfSets--;
                return true;
            }

            private boolean union(int n1, int n2) {
                return _union(root(n1), root(n2));
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[8192];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public InputReader(FileInputStream file) {
            this.stream = file;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res = (res << 3) + (res << 1) + c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

    }

    static class Util {
        public static int mod(int a, int mod) {
            int res = a % mod;
            return res >= 0 ? res : res + mod;
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;
        private ArrayList<String> res = new ArrayList<>();
        private StringBuilder sb = new StringBuilder("");

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer, false);
        }

        public void println() {
            res.add(sb.toString());
            sb = new StringBuilder("");
        }

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                sb.append(objects[i]);
            }
        }

        public void close() {
            writer.flush();
            writer.close();
        }

        public void flush() {
            for (String str : res) writer.printf("%s\n", str);
            res.clear();
            sb = new StringBuilder("");
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.io.InputStream;

public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        CTheLabyrinth solver = new CTheLabyrinth();
        solver.solve(1, in, out);
        out.close();
    }

    static class CTheLabyrinth {
        private int n;
        private int m;

        private int index(int i, int j) {
            return i * m + j;
        }

        private boolean isValid(int x, int y) {
            return x >= 0 && x < n && y >= 0 && y < m;
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            n = in.nextInt();
            m = in.nextInt();
            char[][] arr = new char[n][m];
            CTheLabyrinth.DisjointSet ds = new CTheLabyrinth.DisjointSet(n * m);
            for (int i = 0; i < n; i++) {
                String str = in.nextLine();
                for (int j = 0; j < m; j++) arr[i][j] = str.charAt(j);
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (arr[i][j] == '.') {
                        if (isValid(i - 1, j) && arr[i - 1][j] == '.') ds.union(index(i, j), index(i - 1, j));
                        if (isValid(i, j + 1) && arr[i][j + 1] == '.') ds.union(index(i, j), index(i, j + 1));
                        if (isValid(i + 1, j) && arr[i + 1][j] == '.') ds.union(index(i, j), index(i + 1, j));
                        if (isValid(i, j - 1) && arr[i][j - 1] == '.') ds.union(index(i, j), index(i, j - 1));
                    }
                }
            }
            ArrayList<String> ans = new ArrayList<>();
            StringBuilder sb;
            HashSet<Integer> hs = new HashSet<>();
            for (int i = 0; i < n; i++) {
                sb = new StringBuilder();
                for (int j = 0; j < m; j++) {
                    if (arr[i][j] == '.') sb.append('.');
                    else {
                        hs.clear();
                        if (isValid(i - 1, j) && arr[i - 1][j] == '.') hs.add(ds.root(index(i - 1, j)));
                        if (isValid(i, j + 1) && arr[i][j + 1] == '.') hs.add(ds.root(index(i, j + 1)));
                        if (isValid(i + 1, j) && arr[i + 1][j] == '.') hs.add(ds.root(index(i + 1, j)));
                        if (isValid(i, j - 1) && arr[i][j - 1] == '.') hs.add(ds.root(index(i, j - 1)));
                        int res = 0;
                        for (int x : hs) res += ds.setSize[x];
                        res++;
                        res = Util.mod(res, 10);
                        sb.append(res);
                    }

                }
                ans.add(sb.toString());
            }
            for (String str : ans) out.printf("%s\n", str);
        }

        static class DisjointSet {
            private int[] parent;
            private int[] setSize;
            private int numOfSets;

            private DisjointSet(int n) {
                parent = new int[n];
                setSize = new int[n];
                for (int i = 0; i < n; i++) {
                    setSize[i] = 1;
                    parent[i] = i;
                }
                numOfSets = n;
            }

            private int root(int x) {
                if (x == parent[x]) return x;
                return parent[x] = root(parent[x]);
            }

            private boolean _union(int n1, int n2) {
                if (n1 == n2) return false;
                if (setSize[n1] > setSize[n2]) {
                    parent[n2] = parent[n1];
                    setSize[n1] += setSize[n2];
                } else {
                    parent[n1] = parent[n2];
                    setSize[n2] += setSize[n1];
                }
                numOfSets--;
                return true;
            }

            private boolean union(int n1, int n2) {
                return _union(root(n1), root(n2));
            }

        }

    }

    static class Util {
        public static int mod(int a, int mod) {
            int res = a % mod;
            return res >= 0 ? res : res + mod;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[8192];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public InputReader(FileInputStream file) {
            this.stream = file;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res = (res << 3) + (res << 1) + c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.FileInputStream;
import java.util.HashSet;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        CTheLabyrinth solver = new CTheLabyrinth();
        solver.solve(1, in, out);
        out.close();
    }

    static class CTheLabyrinth {
        private int n;
        private int m;

        private int index(int i, int j) {
            return i * m + j;
        }

        private boolean isValid(int x, int y) {
            return x >= 0 && x < n && y >= 0 && y < m;
        }

        public void solve(int testNumber, InputReader in, OutputWriter out) {
            n = in.nextInt();
            m = in.nextInt();
            char[][] arr = new char[n][m];
            CTheLabyrinth.DisjointSet ds = new CTheLabyrinth.DisjointSet(n * m);
            for (int i = 0; i < n; i++) {
                String str = in.nextLine();
                for (int j = 0; j < m; j++) arr[i][j] = str.charAt(j);
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (arr[i][j] == '.') {
                        if (isValid(i - 1, j) && arr[i - 1][j] == '.') ds.union(index(i, j), index(i - 1, j));
                        if (isValid(i, j + 1) && arr[i][j + 1] == '.') ds.union(index(i, j), index(i, j + 1));
                        if (isValid(i + 1, j) && arr[i + 1][j] == '.') ds.union(index(i, j), index(i + 1, j));
                        if (isValid(i, j - 1) && arr[i][j - 1] == '.') ds.union(index(i, j), index(i, j - 1));
                    }
                }
            }
            HashSet<Integer> hs = new HashSet<>();
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (arr[i][j] == '.') out.print('.');
                    else {
                        hs.clear();
                        if (isValid(i - 1, j) && arr[i - 1][j] == '.') hs.add(ds.root(index(i - 1, j)));
                        if (isValid(i, j + 1) && arr[i][j + 1] == '.') hs.add(ds.root(index(i, j + 1)));
                        if (isValid(i + 1, j) && arr[i + 1][j] == '.') hs.add(ds.root(index(i + 1, j)));
                        if (isValid(i, j - 1) && arr[i][j - 1] == '.') hs.add(ds.root(index(i, j - 1)));
                        int res = 0;
                        for (int x : hs) res += ds.setSize[x];
                        res++;
                        res = Util.mod(res, 10);
                        out.print(res);
                    }

                }
                out.printLine();
            }
        }

        static class DisjointSet {
            private int[] parent;
            private int[] setSize;
            private int numOfSets;

            private DisjointSet(int n) {
                parent = new int[n];
                setSize = new int[n];
                for (int i = 0; i < n; i++) {
                    setSize[i] = 1;
                    parent[i] = i;
                }
                numOfSets = n;
            }

            private int root(int x) {
                if (x == parent[x]) return x;
                return parent[x] = root(parent[x]);
            }

            private boolean _union(int n1, int n2) {
                if (n1 == n2) return false;
                if (setSize[n1] > setSize[n2]) {
                    parent[n2] = parent[n1];
                    setSize[n1] += setSize[n2];
                } else {
                    parent[n1] = parent[n2];
                    setSize[n2] += setSize[n1];
                }
                numOfSets--;
                return true;
            }

            private boolean union(int n1, int n2) {
                return _union(root(n1), root(n2));
            }

        }

    }

    static class Util {
        public static int mod(int a, int mod) {
            int res = a % mod;
            return res >= 0 ? res : res + mod;
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[8192];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public InputReader(FileInputStream file) {
            this.stream = file;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res = (res << 3) + (res << 1) + c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String nextLine() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndOfLine(c));
            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        private boolean isEndOfLine(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
            writer.flush();
        }

        public void printLine(Object... objects) {
            print(objects);
            writer.println();
            writer.flush();
        }

        public void close() {
            writer.close();
        }

    }
}


import java.util.*;
import java.io.*;

public class ForProblems {

    Random rnd = new Random();

    int[][] visit;
    char[][] a;
    int n, m;
    int ans = 0;
    int[] dx = {0, 0, 1, -1};
    int[] dy = {1, -1, 0, 0};

    void dfs(int x, int y) {

        visit[x][y] = 1;
        for (int i = 0; i < 4; ++i) {
            int curX = x + dx[i];
            int curY = y + dy[i];
            if (curX >= 0 && curX < n && curY >= 0 && curY < m && visit[curX][curY] == 0 && a[curX][curY] == '.') {
                ++ans;
                dfs(curX, curY);
            }
        }
    }

    void solve() throws IOException {

        n = readInt();
        m = readInt();
        a = new char[n][m];
        visit = new int[n][m];
        for (int i = 0; i < n; ++i) {
            String tmp = readString();
            for (int j = 0; j < m; ++j)
                a[i][j] = tmp.charAt(j);
        }

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (a[i][j] == '*') {
                    dfs(i, j);
                    out.print((ans + 1) % 10);
                    ans = 0;
                    visit = new int[n][m];
                } else
                    out.print(".");
            }
            out.println("");
        }
    }

    final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");

    public static void main(String[] args){
        new ForProblems().run();
    }

    public void run(){
        try{
            long timeBegin = System.currentTimeMillis();
            Locale.setDefault(Locale.US);

            init();
            solve();

            out.close();

            long timeEnd = System.currentTimeMillis();
            System.err.println("Time = " + (timeEnd - timeBegin));
        }catch (Exception e){
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }

    String readString() throws IOException{
        while(!tok.hasMoreTokens()){
            try{
                tok = new StringTokenizer(in.readLine());
            }catch (Exception e){
                return null;
            }
        }

        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    void init() throws FileNotFoundException{
        Locale.setDefault(Locale.US);

        if (ONLINE_JUDGE){
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        } else {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        }
    }
}


import java.util.*;
import java.io.*;

public class ForProblems {

    Random rnd = new Random();

    int[][] visit;
    char[][] a;
    int n, m;
    int ans = 0;

    void dfs(int i, int j) {

        visit[i][j] = 1;
        if (i - 1 >= 0 && visit[i - 1][j] == 0 && a[i - 1][j] == '.') {
            ++ans;
            dfs(i - 1, j);
        }
        if (i + 1 < n && visit[i + 1][j] == 0 && a[i + 1][j] == '.') {
            ++ans;
            dfs(i + 1, j);
        }
        if (j - 1 >= 0 && visit[i][j - 1] == 0 && a[i][j - 1] == '.') {
            ++ans;
            dfs(i, j - 1);
        }
        if (j + 1 < m && visit[i][j + 1] == 0 && a[i][j + 1] == '.') {
            ++ans;
            dfs(i, j + 1);
        }
    }

    void solve() throws IOException {

        n = readInt();
        m = readInt();
        a = new char[n][m];
        visit = new int[n][m];
        for (int i = 0; i < n; ++i) {
            String tmp = readString();
            for (int j = 0; j < m; ++j)
                a[i][j] = tmp.charAt(j);
        }

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (a[i][j] == '*') {
                    dfs(i, j);
                    out.print(ans + 1);
                    ans = 0;
                    visit = new int[n][m];
                } else
                    out.print(".");
            }
            out.println("");
        }
    }

    final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");

    public static void main(String[] args){
        new ForProblems().run();
    }

    public void run(){
        try{
            long timeBegin = System.currentTimeMillis();
            Locale.setDefault(Locale.US);

            init();
            solve();

            out.close();

            long timeEnd = System.currentTimeMillis();
            System.err.println("Time = " + (timeEnd - timeBegin));
        }catch (Exception e){
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }

    String readString() throws IOException{
        while(!tok.hasMoreTokens()){
            try{
                tok = new StringTokenizer(in.readLine());
            }catch (Exception e){
                return null;
            }
        }

        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    void init() throws FileNotFoundException{
        Locale.setDefault(Locale.US);

        if (ONLINE_JUDGE){
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        } else {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        }
    }
}


import java.util.*;
import java.io.*;

public class ForProblems {

    Random rnd = new Random();

    int[][] visit;
    char[][] a;
    int n, m;
    int comp = 1;
    int[] dx = {0, 0, 1, -1};
    int[] dy = {1, -1, 0, 0};
    int[] size;

    void dfs(int x, int y) {

        visit[x][y] = comp;
        size[comp]++;
        for (int i = 0; i < 4; ++i) {
            int curX = x + dx[i];
            int curY = y + dy[i];
            if (curX >= 0 && curX < n && curY >= 0 && curY < m && visit[curX][curY] == 0 && a[curX][curY] == '.') {
                dfs(curX, curY);
            }
        }
    }

    void solve() throws IOException {

        n = readInt();
        m = readInt();
        a = new char[n][m];
        visit = new int[n][m];
        size = new int[n * m];
        for (int i = 0; i < n; ++i) {
            String tmp = readString();
            for (int j = 0; j < m; ++j)
                a[i][j] = tmp.charAt(j);
        }

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (a[i][j] == '.' && visit[i][j] == 0) {
                    dfs(i, j);
                    ++comp;
                }
            }
        }

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (a[i][j] == '*') {
                    Set<Integer> tmp = new TreeSet<Integer>();
                    for (int q = 0; q < 4; ++q) {
                        int curX = i + dx[q];
                        int curY = j + dy[q];
                        if (curX >= 0 && curX < n && curY >= 0 && curY < m && a[curX][curY] == '.')
                            tmp.add(visit[curX][curY]);
                    }
                    int ans = 0;
                    for (Integer t : tmp) {
                        ans += size[t];
                    }
                    out.print((ans + 1) % 10);
                } else
                    out.print('.');
            }
            out.println();
        }
    }

    final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");

    public static void main(String[] args){
        new ForProblems().run();
    }

    public void run(){
        try{
            long timeBegin = System.currentTimeMillis();
            Locale.setDefault(Locale.US);

            init();
            solve();

            out.close();

            long timeEnd = System.currentTimeMillis();
            System.err.println("Time = " + (timeEnd - timeBegin));
        }catch (Exception e){
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }

    String readString() throws IOException{
        while(!tok.hasMoreTokens()){
            try{
                tok = new StringTokenizer(in.readLine());
            }catch (Exception e){
                return null;
            }
        }

        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    void init() throws FileNotFoundException{
        Locale.setDefault(Locale.US);

        if (ONLINE_JUDGE){
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        } else {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        }
    }
}


import java.util.*;
import java.io.*;

public class ForProblems {

    Random rnd = new Random();

    int[][] visit;
    char[][] a;
    int n, m;
    int ans = 0;

    void dfs(int i, int j) {

        visit[i][j] = 1;
        if (i - 1 >= 0 && visit[i - 1][j] == 0 && a[i - 1][j] == '.') {
            ++ans;
            dfs(i - 1, j);
        }
        if (i + 1 < n && visit[i + 1][j] == 0 && a[i + 1][j] == '.') {
            ++ans;
            dfs(i + 1, j);
        }
        if (j - 1 >= 0 && visit[i][j - 1] == 0 && a[i][j - 1] == '.') {
            ++ans;
            dfs(i, j - 1);
        }
        if (j + 1 < m && visit[i][j + 1] == 0 && a[i][j + 1] == '.') {
            ++ans;
            dfs(i, j + 1);
        }
    }

    void solve() throws IOException {

        n = readInt();
        m = readInt();
        a = new char[n][m];
        visit = new int[n][m];
        for (int i = 0; i < n; ++i) {
            String tmp = readString();
            for (int j = 0; j < m; ++j)
                a[i][j] = tmp.charAt(j);
        }

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                if (a[i][j] == '*') {
                    dfs(i, j);
                    out.print((ans + 1) % 10);
                    ans = 0;
                    visit = new int[n][m];
                } else
                    out.print(".");
            }
            out.println("");
        }
    }

    final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");

    public static void main(String[] args){
        new ForProblems().run();
    }

    public void run(){
        try{
            long timeBegin = System.currentTimeMillis();
            Locale.setDefault(Locale.US);

            init();
            solve();

            out.close();

            long timeEnd = System.currentTimeMillis();
            System.err.println("Time = " + (timeEnd - timeBegin));
        }catch (Exception e){
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }

    String readString() throws IOException{
        while(!tok.hasMoreTokens()){
            try{
                tok = new StringTokenizer(in.readLine());
            }catch (Exception e){
                return null;
            }
        }

        return tok.nextToken();
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    void init() throws FileNotFoundException{
        Locale.setDefault(Locale.US);

        if (ONLINE_JUDGE){
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        } else {
            in = new BufferedReader(new FileReader("input.txt"));
            out = new PrintWriter("output.txt");
        }
    }
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <string>
#include <string.h>
#include <map>
#include <vector>
#include <fstream>
using namespace std;
#define N 302400
#define M 1000000
//#define MAXN 1125899906842624L
#define MAXVAL 1e15L
#define MAXN 520*520
#define MOD 1000000007
int n, m;
long long a[N];
long long WF = ((long long)1<<(long long)32)-(long long)1;
void solve() {
	int i, x;
	char str[10];
	m = 0;
	long long ans = 0;
	long long v = 1;
	bool flag = true;
	a[0] = 1;
	for (i = 1; i <= n; i++) {
		scanf(" %s", &str);
		if (str[0] == 'f') {
			scanf(" %d", &x);
			m++;
			v *= (long long)x;
			a[m] = v;
		} else if (str[0] == 'e') {
			m--;
			v = a[m];
		} else {
			ans += v;
			if (ans > WF) flag = false;
		}
	}
	if (flag && ans <= WF)
		printf("%lld\n", ans);
	else
		printf("OVERFLOW!!!\n");
}

int main() {

	while (scanf("%d", &n) != EOF) {
		solve();
	}
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <string>
#include <string.h>
#include <map>
#include <vector>
#include <fstream>
using namespace std;
#define N 302400
#define M 1000000
//#define MAXN 1125899906842624L
#define MAXVAL 1e15L
#define MAXN 520*520
#define MOD 1000000007
int n, m;
long long a[N];
bool isOverFlow[N];
long long WF = ((long long)1<<(long long)32)-(long long)1;
void solve() {
	int i, j, x;
	char str[10];
	m = 0;
	long long ans = 0;
	long long v = 1;
	bool flag = true;
	a[m] = 1;

	for (i = 1; i <= n; i++) {
		scanf(" %s", &str);
		if (str[0] == 'f') {
			scanf(" %d", &x);
			m++;
			if (v * (long long)x > WF || isOverFlow[m-1]) {
				isOverFlow[m] = 1;
			} else {
				isOverFlow[m] = 0;
				v *= (long long)x;
			}
			a[m] = v;
		} else if (str[0] == 'e') {
			m--;
			v = a[m];
		} else {
			if (isOverFlow[m]) {
				flag = 0;
			}
			ans += v;
			if (ans > WF) flag = false;
		}
	}
	if (flag && ans <= WF)
		printf("%lld\n", ans);
	else
		printf("OVERFLOW!!!\n");
}

int main() {

	while (scanf("%d", &n) != EOF) {
		solve();
	}
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <string>
#include <string.h>
#include <map>
#include <vector>
#include <fstream>
using namespace std;
#define N 302400
#define M 1000000
//#define MAXN 1125899906842624L
#define MAXVAL 1e15L
#define MAXN 520*520
#define MOD 1000000007
int n, m;
int a[N];
bool isOverFlow[N];
long long WF = ((long long)1<<(long long)32)-(long long)1;
void solve() {
	int i, j, x;
	char str[10];
	m = 0;
	long long ans = 0;
	long long v = 1;
	bool flag = true;
	a[m] = 1;
	for (i = 1; i <= n; i++) {
		scanf(" %s", &str);
		if (str[0] == 'f') {
			scanf(" %d", &x);
			m++;
			if (v * (long long)x > WF) {
				isOverFlow[m] = 1;
			} else {
				isOverFlow[m] = 0;
				v *= (long long)x;
			}
			a[m] = v;
		} else if (str[0] == 'e') {
			m--;
			v = a[m];
		} else {
			if (isOverFlow[m]) {
				flag = 0;
			}
			ans += v;
		}
	}
	if (flag && ans <= WF)
		printf("%lld\n", ans);
	else
		printf("OVERFLOW!!!\n");
}

int main() {

	while (scanf("%d", &n) != EOF) {
		solve();
	}
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <string>
#include <string.h>
#include <map>
#include <vector>
#include <fstream>
using namespace std;
#define N 302400
#define M 1000000
//#define MAXN 1125899906842624L
#define MAXVAL 1e15L
#define MAXN 520*520
#define MOD 1000000007
int n, m;
int a[N];
bool isOverFlow[N];
long long WF = ((long long)1<<(long long)32)-(long long)1;
void solve() {
	int i, j, x;
	char str[10];
	m = 0;
	long long ans = 0;
	long long v = 1;
	bool flag = true;
	a[m] = 1;
	for (i = 1; i <= n; i++) {
		scanf(" %s", &str);
		if (str[0] == 'f') {
			scanf(" %d", &x);
			m++;
			if (v * (long long)x > WF) {
				isOverFlow[m] = 1;
			} else {
				isOverFlow[m] = 0;
				v *= (long long)x;
			}
			a[m] = v;
		} else if (str[0] == 'e') {
			m--;
			v = a[m];
		} else {
			if (isOverFlow[m]) {
				flag = 0;
			}
			ans += v;
		}
	}
	if (flag && ans < WF)
		printf("%lld\n", ans);
	else
		printf("OVERFLOW!!!\n");
}

int main() {

	while (scanf("%d", &n) != EOF) {
		solve();
	}
	return 0;
}


#include <iostream>
#include <iomanip>
#include <tchar.h>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <set>

using namespace std;

typedef long long ll;
ll loops[100000];

ll MAXVAL = 1LL << 32;

int _tmain(int argc, _TCHAR* argv[])
{
	ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
//	freopen("D:\\Projects\\CPP\\UVa\\CF522\\a.in", "r",stdin);
//	freopen("D:\\Projects\\CPP\\UVa\\CF522\\a.out", "w",stdout);
#else
//	freopen("input.txt","r",stdin);
//	freopen("output.txt", "w",stdout);
#endif
        int n;
        cin >> n;
        loops[0] = 1;
        int cnt = 0;
        ll x = 0;
        for (int q = 0; q < n && x < MAXVAL; ++q) {
		string s;
		cin >> s;
		if (s == "for") {
		  int v;
		  cin >> v;
                  loops[cnt + 1] = min(loops[cnt] * v, MAXVAL);
                  cnt++;
                } else if (s == "end") {
		  cnt--;
                } else if (s == "add") {
                  x += loops[cnt];
                };
        }
        if (x < MAXVAL)
		cout << x << endl;
	else
		cout << "OVERFLOW!!!" << endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdlib>

using namespace std;

const long long infty = 1000000000000000LL;
const long long onbound = ((1LL << 32LL) - 1LL);

long long rr[1000000];
long long forc[1000000];

char s[100];

int main() {
	int t;
	cin >> t;
	int curblock = 0;

	for (int i = 0; i < t; ++i) {
		scanf("%s", &s);
		forc[0] = 1LL;
		if (s[0] == 'a') {
			rr[curblock]++;
			if (rr[curblock] > onbound) {
				rr[curblock] = infty;
			}
		}
		else if (s[0] == 'e') {
			if (rr[curblock] > onbound) {
				rr[curblock] = infty;
			}
			else {
				rr[curblock] *= forc[curblock];
				if (rr[curblock] > onbound) {
					rr[curblock] = infty;
				}

			}
			rr[curblock - 1] += rr[curblock];
			--curblock;
			if (rr[curblock] > onbound) {
				rr[curblock] = infty;
			}
		}
		else {
			int cc;
			scanf("%d", &cc);
			curblock++;
			rr[curblock] = 0;
			forc[curblock] = cc;
		}
	}
	if (rr[curblock] > onbound) cout << "OVERFLOW!!!" << endl; else
	cout << rr[curblock];
}


#include<cstdio>
#include<stack>
#include<algorithm>
using namespace std;
typedef __int64 ll;
const int maxn=1e5+50;
int main()
{
	int i,j,t,judge;
	stack<ll> sta;
	ll ans,psum,n;
	ll one=1;
	ll up=(one<<32)-1;
	scanf("%d",&t);
	ans=0;judge=1;
	for(i=1;i<=t;i++)
	{
		char s[10];
		scanf("%s",s);
		if(s[0]=='f')
		{
			scanf("%I64d",&n);
			if(ans!=0)sta.push(ans);ans=0;
			sta.push(-n);
		}
		else if(s[0]=='a')
		{
			ans++;
			if(ans>up)judge=0;
		}
		else if(s[0]=='e')
		{
			n=sta.top();sta.pop();
			ans*=(-n);
			if(ans>up)judge=0;
		}
	}
	while(!sta.empty()){
		ans+=sta.top();sta.pop();
		if(ans>up)
		{
			judge=0;break;
		}
	}
	if(!judge)printf("OVERFLOW!!!\n");
	else printf("%I64d\n",ans);
}


#include<cstdio>
#include<stack>
#include<algorithm>
using namespace std;
typedef __int64 ll;
const int maxn=1e5+50;
int main()
{
	int i,j,t,judge;
	stack<ll> sta;
	ll ans,psum,n;
	ll one=1;
	ll up=(one<<32)-1;
	scanf("%d",&t);
	ans=0;judge=1;
	for(i=1;i<=t;i++)
	{
		char s[10];
		scanf("%s",s);
		if(s[0]=='f')
		{
			scanf("%I64d",&n);
			if(ans!=0)sta.push(ans);ans=0;
			sta.push(-n);
		}
		else if(s[0]=='a')
		{
			ans++;
			if(ans>up)judge=0;
		}
		else if(s[0]=='e')
		{
			n=sta.top();sta.pop();
			while(n>0)
			{
				ans+=n;
				n=sta.top();sta.pop();
			}
			ans*=(-n);
			if(ans>up)judge=0;
		}
	}
	while(!sta.empty()){
		ans+=sta.top();sta.pop();
		if(ans>up)
		{
			judge=0;break;
		}
	}
	if(!judge)printf("OVERFLOW!!!\n");
	else printf("%I64d\n",ans);
}


n=int(input(""))
l=[]
m=0
for i in range(n):
    k=input("")
    if k=='add':
        l.append(1)
    elif k=='end':
        l.append('E')
    else:
        x,y=k.split()
        l.append(int(y))
        l.append('F')
i=0
n=len(l)
while i!=n:
    if l[i]=='E':
        m=0
        for j in range(i-1,-1,-1):
            if l[j]=='F':
                m=m*l[j-1]
                l[j-1]=m
                if m > 2 ** 32 - 1:
                    print("OVERFLOW!!!")
                    exit()
                break
            else:
                m+=l[j]
        del l[i:j-1:-1]
        n=n-(i-j+1)
        i=j-1
    i+=1
j=0
for items in l:
    j+=items
if j>2**32-1:
    print("OVERFLOW!!!")
else:
    print(j)


n=int(input(""))
l=[]
m=0
i=0
p=0
y=0
while i!=n:
    k=input("")
    if k=='add':
        l.append(1)
        i+=1
        p+=1
    elif k=='end':
        m=0
        for j in range(p-1,-1,-1):
            if l[j]=='F':
                m=m*l[j-1]
                l[j-1]=m
                if m > 2 ** 32 - 1:
                    y=1
                break
            else:
                m+=l[j]
        del l[p-1:j-1:-1]
        p=j
        i+=1
    else:
        x,y=k.split()
        l.append(int(y))
        l.append('F')
        p += 2
        i+=1
j=0
for items in l:
    j+=items
if y==1 or j>2**32-1:
    print("OVERFLOW!!!")
else:
    print(j)


n=int(input(""))
l=[]
m=0
i=0
p=0
y=0
while i!=n and y!=1:
    k=input("")
    if k=='add':
        l.append(1)
        i+=1
        p+=1
    elif k=='end':
        m=0
        for j in range(p-1,-1,-1):
            if l[j]=='F':
                m=m*l[j-1]
                l[j-1]=m
                if m > 2 ** 32 - 1:
                    y=1
                break
            else:
                m+=l[j]
        del l[p-1:j-1:-1]
        p=j
        i+=1
    else:
        x,y=k.split()
        l.append(int(y))
        l.append('F')
        p += 2
        i+=1
for i in range(n-i):
    k=input("")
j=0
for items in l:
    j+=items
if y==1 or j>2**32-1:
    print("OVERFLOW!!!")
else:
    print(j)


n=int(input(""))
l=[]
m=0
for i in range(n):
    k=input("")
    if k=='add':
        l.append(1)
    elif k=='end':
        l.append('E')
    else:
        x,y=k.split()
        l.append(int(y))
        l.append('F')
i=0
n=len(l)
while i!=n:
    if l[i]=='E':
        m=0
        for j in range(i-1,-1,-1):
            if l[j]=='F':
                m=m*l[j-1]
                l[j-1]=m
                break
            else:
                m+=l[j]
        del l[i:j-1:-1]
        n=n-(i-j+1)
        i=j-1
    i+=1
j=0
for items in l:
    j+=items
if j>2**32-1:
    print("OVERFLOW!!!")
else:
    print(j)


n = int(input())
MAX = 2**32 - 1
st = []
last = [1]
c = 0
f = False

for i in range(n):
    s = input()
    if s[0] == 'f':
        last.append(min(last[-1]*int(s[4:]), MAX+1))
    elif s[0] == 'e':
        last.pop()
    else:
        c += last[-1]
        if c > MAX:
            f = True
            break

print('OVERFLOW!!!') if f else print(c)


n = int(input())
MAX = 2**32 - 1
st = 1
last = []
c = 0
f = False

for i in range(n):
    s = input().split()
    if s[0] == 'for':
        st *= int(s[1])
        last.append(int(s[1]))
    elif s[0] == 'end':
        st = st//last[-1]
        last.pop()
    else:
        c += st
        if c > MAX:
            f = True
            break

print('OVERFLOW!!!') if f else print(c)


import math
import sys

#imgur.com/Pkt7iIf.png

#n, m = map(int, input().split())
#n = int(input())
#d = list(map(int, input().split()))

n = int(input())
MAX = 2**32 - 1
st = 1
last = []
c = 0
f = False

for i in range(n):
    if c > MAX:
        f = True
        break

    s = input().split()

    if s[0] == 'add':
        c += st

    elif s[0] == 'for':
        st *= int(s[1])
        last.append(int(s[1]))

    else:
        st = st//last[-1]
        last.pop()

print('OVERFLOW!!!') if f else print(c)


import math
import sys

#imgur.com/Pkt7iIf.png

#n, m = map(int, input().split())
#n = int(input())
#d = list(map(int, input().split()))

n = int(input())
MAX = 2**32 - 1
st = 1
last = []
c = 0
f = False

for i in range(n):
    if c > MAX:
        f = True
        break

    s = input().split()

    if s[0] == 'add' and st == 0:
        c += 1
        continue

    if s[0] == 'add' and st != 0:
        c += st
        continue

    if s[0] == 'for':
        st *= int(s[1])
        last.append(int(s[1]))
        continue

    if s[0] == 'end':
        st = st//last[-1]
        last.pop()
        continue

print('OVERFLOW!!!') if f else print(c)


n = int(input())
MAX = 2**32 - 1
st = 1
last = []
c = 0
f = False

for i in range(n):
    s = input()
    if s[0] == 'f':
        t = int(s[4:])
        st *= t
        last.append(t)
    elif s[0] == 'e':
        st = st//last[-1]
        last.pop()
    else:
        c += st
        if c > MAX:
            f = True
            break

print('OVERFLOW!!!') if f else print(c)


n = int(input())
MAX = 2**32 - 1
st = []
last = [1]
c = 0
f = False

for i in range(n):
    s = input()
    if s[0] == 'f':
        last.append(last[-1]*int(s[4:]))
    elif s[0] == 'e':
        last.pop()
    else:
        c += last[-1]
        if c > MAX:
            f = True
            break

print('OVERFLOW!!!') if f else print(c)


x = 0
n = int(input())
cur = 1
stack = []
error = 0
for i in range(n):
    s = input().split()
    if s[0]=='add':
        if error!=0: print('OVERFLOW!!!'); break
        x+=cur
        if x>=2**32: print('OVERFLOW!!!'); break
    elif s[0]=='for':
        if cur*int(s[1])<2**32:
            cur*=int(s[1])
            stack.append(int(s[1]))
        else: error+=1
    else:
        if error==0: cur//=stack.pop()
        else: error-=1
else: print(x)


x = 0
n = int(input())
cur = 1
stack = []
error = 0
flag = 0
for i in range(n):
    s = input().split()
    if s[0]=='add':
        if error!=0: print('OVERFLOW!!!'); break
        x+=cur
        if x>=2**32: print('OVERFLOW!!!'); break
    elif s[0]=='for':
        if flag==1: error+=1
        elif cur*int(s[1])<2**32:
            cur*=int(s[1])
            stack.append(int(s[1]))
        else: flag = 1; error+=1
    else:
        if error==0: cur//=stack.pop()
        else:
            error-=1
            if error==0: flag = 0
else: print(x)


x = 0
n = int(input())
cur = 1
stack = []
for i in range(n):
    s = input().split()
    if s[0]=='add':
        x+=cur
        if x>=2**32: print('OVERFLOW!!!'); break
    elif s[0]=='for':
        cur*=int(s[1])
        stack.append(int(s[1]))
    else:
        cur//=stack.pop()
else: print(x)


x = 0
n = int(input())
cur = 1
stack = []
for i in range(n):
    s = input().split()
    if s[0]=='add':
        x+=cur
        if x>=2**32: print('OVERFLOW!!!'); break
    elif s[0]=='for':
        cur = min(2**32, cur*int(s[1]))
        stack.append(int(s[1]))
    else:
        cur//=stack.pop()
else: print(x)


x = 0
n = int(input())
cur = 1
stack = []
for i in range(n):
    s = input().split()
    if s[0]=='add':
        x+=cur
        if x>=2**32: print('OVERFLOW!!!'); break
    elif s[0]=='for':
        cur*=int(s[1])
        if cur>=2**32: print('OVERFLOW!!!'); break
        stack.append(int(s[1]))
    else:
        cur//=stack.pop()
else: print(x)


x = 0
n = int(input())
cur = 1
stack = []
error = 0
for i in range(n):
    s = input().split()
    if s[0]=='add':
        x+=cur
        if x>=2**32: print('OVERFLOW!!!'); break
    elif s[0]=='for':
        if cur*int(s[1])<2**32:
            cur*=int(s[1])
            stack.append(s[1])
        else: error+=1
    else:
        if error==0: cur//=stack.pop()
        else: error-=1
else: print(x)


x = 0
n = int(input())
cur = 1
stack = []
for i in range(n):
    s = input().split()
    if s[0]=='add':
        x+=cur
    elif s[0]=='for':
        cur*=int(s[1])
        stack.append(int(s[1]))
    else:
        cur//=stack.pop()
if x>=2**32: print('OVERFLOW!!!')
else: print(x)


x = 0
n = int(input())
cur = 1
stack = []
for i in range(n):
    s = input().split()
    if s[0]=='add':
        x+=cur
        if x>=2**32: print('OVERFLOW!!!'); break
    elif s[0]=='for':
        cur*=int(s[1])
        if x>=2**32: print('OVERFLOW!!!'); break
        stack.append(int(s[1]))
    else:
        cur//=stack.pop()
else: print(x)


x = 0
n = int(input())
cur = 1
stack = []
error = 0
for i in range(n):
    s = input().split()
    if s[0]=='add':
        if x>=2**32 or error!=0: print('OVERFLOW!!!'); break
        x+=cur
    elif s[0]=='for':
        if cur*int(s[1])<2**32:
            cur*=int(s[1])
            stack.append(int(s[1]))
        else: error+=1
    else:
        if error==0: cur//=stack.pop()
        else: error-=1
else: print(x)


import sys
p = 2 ** 32 - 1
num = 0
st = [1]
for _ in range(int(input())):
    command = input().split()
    if command[0] == "add":
        if st[-1] == -1:
            print("OVERFLOW!!!")
            sys.exit()
        else:
            num += st[-1]
    elif command[0] == "for":
        if st[-1] > p:
            st.append(-1)
        else:
            st.append(int(command[1]) * st[-1])
    elif command[0] == "end":
        st.pop()
print(num) if num < p else print("OVERFLOW!!!")


import sys
p = 2 ** 32 - 1
seq = 0
num = 0
st = [1]
for _ in range(int(input())):
    command = input().split()
    if command[0] == "add":
        num += st[-1]
        if num > p:
            print("OVERFLOW!!!")
            sys.exit()
    elif command[0] == "for":
        if st[-1] > p:
            seq += 1
        else:
            st.append(int(command[1]) * st[-1])
    elif command[0] == "end":
        if seq > 0:
            seq -= 1
        else:
            st.pop()
print(num) if num < p else print("OVERFLOW!!!")


num = 0
st = [1]
for _ in range(int(input())):
    command = input().split()
    if command[0] == "add":
        num += st[-1]
    elif command[0] == "for":
        st.append(int(command[1]) * st[-1])
    elif command[0] == "end":
        st.pop()
print(num) if num < 2 ** 32 - 1 else print("OVERFLOW!!!")


import sys
p = 2 ** 32 - 1
seq = 0
num = 0
st = [1]
for _ in range(int(input())):
    command = input().split()
    if command[0] == "add":
        num += st[-1]
        if num > p:
            print("OVERFLOW!!!")
            sys.exit()
    elif command[0] == "for":
        if st[-1] > p:
            seq += 1
        else:
            st.append(int(command[1]) * st[-1])
    elif command[0] == "end":
        if seq > 0:
            seq -= 1
        else:
            st.pop()
print(num) if num <= p else print("OVERFLOW!!!")


import sys
p = 2 ** 32 - 1
num = 0
st = [1]
for _ in range(int(input())):
    command = input().split()
    if command[0] == "add":
        num += st[-1]
        if num > p:
            print("OVERFLOW!!!")
            sys.exit()
    elif command[0] == "for":
        if st[-1] > p:
            st.append(-1)
        else:
            st.append(int(command[1]) * st[-1])
    elif command[0] == "end":
        st.pop()
print(num) if num < p else print("OVERFLOW!!!")


count = int(input())
opts = []
for i in range(count):
    opts.append(input().split())

forNums = []
stack = [1]
res = 0
overflow_v = 2**32 -1
overflow = False

for i in opts:
    if i[0] == "for":
        for_v = int(i[1])
        forNums.append(for_v)
        stack.append(min(overflow_v+1, for_v * stack[-1]))
    if i[0] == "add":
        res += stack[-1]

    elif i[0] == "end":
        forNums.pop()
        stack.pop()
if res > overflow_v:
    print("OVERFLOW!!!")
    overflow = True
    break
if not overflow:
    print(res)


count = int(input())
opts = []
for i in range(count):
    opts.append(input().split())

forNums = []
stack = []
res = 0
overflow_v = 2**32 -1
overflow = False

for i in opts:
    if i[0] == "for":
        forNums.append(int(i[1]))
    if i[0] == "add":
        temp = 1
        for j in forNums:
            temp*=j
        res += temp
    if res > overflow_v:
        print("OVERFLOW!!!")
        overflow = True
        break
    elif i[0] == "end":
        forNums.pop()

if not overflow:
    print(res)


count = int(input())
opts = []
for i in range(count):
    opts.append(input().split())

forNums = []
stack = []
res = 0
overflow_v = 2**32 -1
overflow = False

for i in opts:
    if i[0] == "for":
        forNums.append(int(i[1]))
    if res > overflow_v:
        print("OVERFLOW!!!")
        overflow = True
        break

    if i[0] == "add":
        temp = 1
        for j in forNums:
            temp*=j
        res += temp
    elif i[0] == "end":
        forNums.pop()

if not overflow:
    print(res)


cnt = (1 << 32)
l = int(input().strip())
ans = 0
array = []
multiply = [1]
for i in range(l):
    line = input().strip()
    if line[0] == 'f':
        multiply.append(min(cnt, multiply[-1]*int(line[3:])))
        array.append(int(line[3:]))
    elif line[0] == 'e':
        multiply.pop()
        array.pop()
    elif line[0] == 'a':
        ans += multiply[-1]
if ans >= cnt:
    print('OVERFLOW!!!')
else:
    print(int(ans))
————————————————
版权声明：本文为CSDN博主「Ariel_Cz1」的原创文章，遵循 CC 4.0 BY-SA 版权协议，转载请附上原文出处链接及本声明。
原文链接：https://blog.csdn.net/qq_28818465/article/details/92252096


count = int(input())
opts = []
for i in range(count):
    opts.append(input().split())

forNums = []
stack = [1]
res = 0
overflow_v = 2**32 -1
overflow = False

for i in opts:
    if i[0] == "for":
        for_v = int(i[1])
        forNums.append(for_v)
        stack.append(min(overflow_v+1, for_v * stack[-1]))
    if i[0] == "add":
        res += stack[-1]
    if res > overflow_v:
        print("OVERFLOW!!!")
        overflow = True
        break
    elif i[0] == "end":
        forNums.pop()
        stack.pop()

if not overflow:
    print(res)


cnt = (1 << 32)
l = int(input().strip())
ans = 0
array = []
multiply = [1]
for i in range(l):
    line = input().strip()
    if line[0] == 'f':
        multiply.append(min(cnt, multiply[-1]*int(line[3:])))
        array.append(int(line[3:]))
    elif line[0] == 'e':
        multiply.pop()
        array.pop()
    elif line[0] == 'a':
        ans += multiply[-1]
if ans >= cnt:
    print('OVERFLOW!!!')
else:
    print(int(ans))


count = int(input())
opts = []
for i in range(count):
    opts.append(input().split())

forNums = []
stack = [1]
res = 0
overflow_v = 2**32 -1
overflow = False

for i in opts:
    if i[0] == "for":
        for_v = int(i[1])
        forNums.append(for_v)
        stack.append(for_v * stack[-1])
    if i[0] == "add":
        res += stack[-1]
    if res > overflow_v:
        print("OVERFLOW!!!")
        overflow = True
        break
    elif i[0] == "end":
        forNums.pop()
        stack.pop()

if not overflow:
    print(res)


count = int(input())
opts = []
for i in range(count):
    opts.append(input().split())

forNums = []
stack = [1]
res = 0
overflow_v = 2**32 -1
overflow = False

for i in opts:
    if i[0] == "for":
        for_v = int(i[1])
        forNums.append(for_v)
        stack.append(min(overflow_v+1, for_v * stack[-1]))
    if i[0] == "add":
        res += stack[-1]

    elif i[0] == "end":
        forNums.pop()
        stack.pop()
if res > overflow_v:
    print("OVERFLOW!!!")
else:
    print(res)


n = int(input())
l = []
for i in range(n):
    k = input().split(' ')
    l.append(k)

ans = 0
i = 0
lo = 2**32-1
flag = 0
while i<n:
    z = l[i]
    if z[0] == 'for':
        ba = []

        if l[i+1][0] == 'for':
            x = i
            t = 1
            b = -1
            count = 0
            while x<n:

                if l[x][0] == 'for' :
                    m = int(l[x][1])
                    ba.append(m)
                    if t<=lo:
                      count+=1
                      t*=m

                elif l[x][0] == 'add':

                    ans+=t
                    if ans>lo:
                        flag = 1
                        break
                elif l[x][0] == 'end':
                    if count == len(ba):
                      t  = t//ba[b]
                    else:
                        count+=1
                    b-=1
                    if b == -len(ba)-1:
                        x+=1
                        break
                x+=1
            i = x

        else:
           if  l[i+1][0] == 'add':
               ans+=int(l[i][1])
               if ans>lo:

                   break
           i+=1

    else:
        if z[0] == 'add':
            i+=1
            ans+=1
            if ans>lo:
                break
        else:
            i+=1
            if ans>lo:
                break
    if flag == 1:

        break

if ans>lo:
    print("OVERFLOW!!!")
else:
 print(ans)


n = int(input())
l = []
for i in range(n):
    k = input().split(' ')
    l.append(k)

ans = 0
i = 0
lo = 2**32-1
flag = 0
while i<n:
    z = l[i]
    if z[0] == 'for':
            ba = []

            x = i
            t = 1
            b = -1
            count = 0
            while x<n:

                if l[x][0] == 'for' :
                    m = int(l[x][1])
                    ba.append(m)
                    if t<=lo:
                      count+=1
                      t*=m

                elif l[x][0] == 'add':

                    ans+=t
                    if ans>lo:
                        flag = 1
                        break
                elif l[x][0] == 'end':
                    if count == len(ba):
                      t  = t//ba[b]
                    else:
                        count+=1
                    b-=1
                    if b == -len(ba)-1:
                        x+=1
                        break
                x+=1
            i = x

    else:
        if z[0] == 'add':
            i+=1
            ans+=1
            if ans>lo:
                break
        else:
            i+=1
            if ans>lo:
                break
    if flag == 1:

        break

if ans>lo:
    print("OVERFLOW!!!")
else:
 print(ans)


n = int(input())
l = []
for i in range(n):
    k = input().split(' ')
    l.append(k)

ans = 0
i = 0
lo = 2**32-1
while i<n:
    z = l[i]
    if z[0] == 'for':
        ba = []

        if l[i+1][0] == 'for':
            x = i
            t = 1
            b = -1
            while x<n:
                if l[x][0] == 'for':
                    ba.append(int(l[x][1]))
                    t*=int(l[x][1])
                elif l[x][0] == 'add':

                    ans+=t
                    if ans>lo:
                        break
                elif l[x][0] == 'end':
                    t = t//ba[b]
                    b-=1
                    if b == -len(ba)-1:
                        x+=1
                        break
                x+=1
            i = x

        else:
           if  l[i+1][0] == 'add':
               ans+=int(l[i][1])
               if ans>lo:
                   break
           i+=1

    else:
        if z[0] == 'add':
            i+=1
            ans+=1
            if ans>lo:
                break
        else:
            i+=1
            if ans>lo:
                break

if ans>lo:
    print("OVERFLOW!!!")
else:
 print(ans)


n = int(input())
l = []
for i in range(n):
    k = input().split(' ')
    l.append(k)

ans = 0
i = 0
lo = 2**32-1
flag = 0
while i<n:
    z = l[i]
    if z[0] == 'for':
        ba = []

        if l[i+1][0] == 'for':
            x = i
            t = 1
            b = -1
            while x<n:
                if l[x][0] == 'for' and t<lo:
                    ba.append(int(l[x][1]))
                    t*=int(l[x][1])

                elif l[x][0] == 'add':

                    ans+=t
                    if ans>lo:
                        flag = 1
                        break
                elif l[x][0] == 'end':
                    t = t//ba[b]
                    b-=1
                    if b == -len(ba)-1:
                        x+=1
                        break
                x+=1
            i = x

        else:
           if  l[i+1][0] == 'add':
               ans+=int(l[i][1])
               if ans>lo:

                   break
           i+=1

    else:
        if z[0] == 'add':
            i+=1
            ans+=1
            if ans>lo:
                break
        else:
            i+=1
            if ans>lo:
                break
    if flag == 1:
        break

if ans>lo:
    print("OVERFLOW!!!")
else:
 print(ans)


n = int(input())
l = []
for i in range(n):
    k = input().split(' ')
    l.append(k)

ans = 0
i = 0
while i<n:
    z = l[i]
    if z[0] == 'for':
        ba = []

        if l[i+1][0] == 'for':
            x = i
            t = 1
            b = -1
            while x<n:
                if l[x][0] == 'for':
                    ba.append(int(l[x][1]))
                    t*=int(l[x][1])
                elif l[x][0] == 'add':

                    ans+=t
                elif l[x][0] == 'end':
                    t = t//ba[b]
                    b-=1
                    if b == -len(ba)-1:
                        x+=1
                        break
                x+=1
            i = x

        else:
           if  l[i+1][0] == 'add':
               ans+=int(l[i][1])
           i+=1

    else:
        if z[0] == 'add':
            i+=1
            ans+=1
        else:
            i+=1

b = 2**32-1
if ans>b:
    print("OVERFLOW!!!")
else:
 print(ans)


n = int(input())
l = []
for i in range(n):
    k = input().split(' ')
    l.append(k)

ans = 0
i = 0
lo = 2**32-1
flag = 0
while i<n:
    z = l[i]
    if z[0] == 'for':
        ba = []

        x = i
        t = 1
        b = -1
        count = 0
        while x<n:

            if l[x][0] == 'for' :
                m = int(l[x][1])
                ba.append(m)
                if t<=lo:
                    count+=1
                    t*=m

            elif l[x][0] == 'add':

                ans+=t

                if ans>lo:
                    flag = 1
                    break
            elif l[x][0] == 'end':
                if count == len(ba):
                    t  = t//ba[-1]
                    ba.pop(-1)
                    count-=1
                else:
                    count+=1

                if len(ba) == 0:
                    x+=1
                    break
            x+=1

        i = x

    else:
        if z[0] == 'add':
            i+=1
            ans+=1
            if ans>lo:
                break
        else:
            i+=1
            if ans>lo:
                break

    if flag == 1:

        break

if ans>lo:
    print("OVERFLOW!!!")
else:
    print(ans)


n = int(input())
l = []
for i in range(n):
    k = input().split(' ')
    l.append(k)

ans = 0
i = 0
lo = 2**32-1
flag = 0
while i<n:
    z = l[i]
    if z[0] == 'for':
        ba = []

        if l[i+1][0] == 'for':
            x = i
            t = 1
            b = -1
            while x<n:
                if l[x][0] == 'for' and t<=lo:
                    ba.append(int(l[x][1]))
                    t*=int(l[x][1])

                elif l[x][0] == 'add':

                    ans+=t
                    if ans>lo:
                        flag = 1
                        break
                elif l[x][0] == 'end':
                    t = t//ba[b]
                    b-=1
                    if b == -len(ba)-1:
                        x+=1
                        break
                x+=1
            i = x

        else:
           if  l[i+1][0] == 'add':
               ans+=int(l[i][1])
               if ans>lo:

                   break
           i+=1

    else:
        if z[0] == 'add':
            i+=1
            ans+=1
            if ans>lo:
                break
        else:
            i+=1
            if ans>lo:
                break
    if flag == 1:
        ans+=1+lo
        break

if ans>lo:
    print("OVERFLOW!!!")
else:
 print(ans)


n = int(input())
l = []
for i in range(n):
    k = input().split(' ')
    l.append(k)

ans = 0
i = 0
lo = 2**32-1
flag = 0
while i<n:
    z = l[i]
    if z[0] == 'for':
        ba = []

        x = i
        t = 1
        b = -1
        count = 0
        while x<n:

            if l[x][0] == 'for' :
                m = int(l[x][1])
                ba.append(m)

                if t<=lo:
                    count+=1
                    t*=m

            elif l[x][0] == 'add':

                ans+=t

                if ans>lo:
                    flag = 1
                    break
            elif l[x][0] == 'end':
                if count == len(ba):
                    t  = t//ba[-1]
                    ba.pop(-1)
                    count-=1
                else:
                    ba.pop(-1)

                if len(ba) == 0:
                    x+=1
                    break
            x+=1

        i = x

# # 4294967295
# print(lo)

    else:
        if z[0] == 'add':
            i+=1
            ans+=1
            if ans>lo:
                break
        else:
            i+=1
            if ans>lo:
                break

    if flag == 1:

        break

if ans>lo:
    print("OVERFLOW!!!")
else:
    print(ans)


n = int(input())
l = []
for i in range(n):
    k = input().split(' ')
    l.append(k)

ans = 0
i = 0
lo = 2**32-1
flag = 0
while i<n:
    z = l[i]
    if z[0] == 'for':
        ba = []

        x = i
        t = 1
        b = -1
        count = 0
        while x<n:

            if l[x][0] == 'for' :
                m = int(l[x][1])
                ba.append(m)
                if t<=lo:
                    count+=1
                    t*=m

            elif l[x][0] == 'add':

                ans+=t

                if ans>lo:
                    flag = 1
                    break
            elif l[x][0] == 'end':
                if count == len(ba):
                    t  = t//ba[-1]
                    ba.pop(-1)
                    count-=1
                else:
                    count+=1

                if len(ba) == 0:
                    x+=1
                    break
            x+=1

        i = x

# # 4294967295
# print(lo)

    else:
        if z[0] == 'add':
            i+=1
            ans+=1
            if ans>lo:
                break
        else:
            i+=1
            if ans>lo:
                break

    if flag == 1:

        break

if ans>=lo:
    print("OVERFLOW!!!")
else:
    print(ans)


n = int(input())
l = []
for i in range(n):
    k = input().split(' ')
    l.append(k)

ans = 0
i = 0
b = 2**32-1
while i<n:
    z = l[i]
    if z[0] == 'for':
        ba = []

        if l[i+1][0] == 'for':
            x = i
            t = 1
            b = -1
            while x<n:
                if l[x][0] == 'for':
                    ba.append(int(l[x][1]))
                    t*=int(l[x][1])
                elif l[x][0] == 'add':

                    ans+=t
                    if ans>b:
                        break
                elif l[x][0] == 'end':
                    t = t//ba[b]
                    b-=1
                    if b == -len(ba)-1:
                        x+=1
                        break
                x+=1
            i = x

        else:
           if  l[i+1][0] == 'add':
               ans+=int(l[i][1])
               if ans>b:
                   break
           i+=1

    else:
        if z[0] == 'add':
            i+=1
            ans+=1
            if ans>b:
                break
        else:
            i+=1
            if ans>b:
                break

if ans>b:
    print("OVERFLOW!!!")
else:
 print(ans)


t = int(input())
a =[]
flag = 0
for i in range(t):
    a.append(input().split())
while ['end'] in a:
    f=0
    q = a.index(['end'])
    k = 1
    del a[q]
    while q-k >=0 and len(a[q-k])<=1:
        if a[q-k]==['add']:

            f+=1
            del a[q-k]
            k+=1
        elif a[q-k]!=['end']:
            f+=a[q-k][0]
            del a[q-k]
            k+=1
    if a[q-k][0]=='for':
        a[q-k][0] = int(a[q-k][1])*f
        if a[q-k][0]+1>2**32:
            flag = 1
            break
        del a[q-k][1]
count = 0

for i in range(len(a)):
    if a[i][0]=='add':
        count+=1
    else:
        count+=a[i][0]
a[0][0]=count

if flag = 1 or a[0][0]+1>2**32:
    print('OVERFLOW!!!')
else:
    print(a[0][0])


t = int(input())
a =[]
flag = 0
for i in range(t):
    a.append(input().split())
while ['end'] in a:
    if flag ==1:
        break
    f=0
    q = a.index(['end'])
    k = 1
    del a[q]
    while q-k >=0 and len(a[q-k])<=1:
        if a[q-k]==['add']:

            f+=1
            del a[q-k]
            k+=1
        elif a[q-k]!=['end']:
            f+=a[q-k][0]
            del a[q-k]
            k+=1
    if a[q-k][0]=='for':
        a[q-k][0] = int(a[q-k][1])*f
        if a[q-k][0]+1>2**32:
            flag = 1
            break
        del a[q-k][1]
count = 0

for i in range(len(a)):
    if a[i][0]=='add':
        count+=1
    else:
        count+=a[i][0]
a[0][0]=count

if flag == 1 or a[0][0]+1>2**32:
    print('OVERFLOW!!!')
else:
    print(a[0][0])


t = int(input())
lim = 2**32
flag = 0
a = []
f = 0
summ = 0
for i in range(t):
    inp = input()
    if inp[0:3] == 'for':
        a.append(int(inp[4:]))
    if inp[0:3] == 'add':
        f = 1
        for s in a:
            f*=s
            if f>=lim:
                flag = 1
                i = t
                break
        if flag ==1:
            break
        summ+=f
        if sum >=lim:
            flag = 1
            break
    if inp[0:3] == 'end':
        a.pop()
if flag == 1:
    print("OVERFLOW!!!")
else:
    print(summ)


t = int(input())
lim = 2**32
flag = 0
a = []
f = 0
sum = 0
for i in range(t):
    inp = input()
    if inp[0:3] == 'for':
        a.append(int(inp[4:]))
    if inp[0:3] == 'add':
        f = 1
        for s in a:
            f*=s
            if f>=lim:
                flag = 1
                i = t
                break
        if flag ==1:
            break
        sum+=f
        if sum >=lim:
            flag = 1
            break
    if inp[0:3] == 'end':
        a.pop()
if flag == 1:
    print("OVERFLOW!!!")
else:
    print(sum)


s=[0]
q=[]
t = int(input()):
for i in range(t):
    c = input()
    if c[0]>'end':
        s.append(0.0);
        q.append(int(c[4:]))
    elif c[0]<'d'
        s[-1]=s[-1]+1
        else s.pop()*t.pop()+s[-1]
print(int(s[0])if s[0]<2**32 else'OVERFLOW!!!')


s = [0]
lim = 2**32
q = []
t = int(input())
for i in range(t):
    c = input()
    if c[0]>'e':
        s.append(0)
        q.append(int(c[4:]))

    elif c[0]<'b':
        s[-1] = s[-1] + 1

    else:
        s[-1] = s.pop()*q.pop()+s[-1]
print(int(s[0]) if s[0]<lim else'OVERFLOW!!!')


s = [0]
lim = 2**32
q = []
t = int(input())
for i in range(t):
    c = input()
    if c[0]>'e':
        s.append(0.0)
        q.append(int(c[4:]))

    elif c[0]<'b':
        s[-1] = s[-1] + 1

    else:
        s[-1] = s.pop()*q.pop()+s[-1]
print(int(s[0]) if s[0]<lim else'OVERFLOW!!!')


s=[0]
q=[]
t = int(input())
for i in range(t):
    c = input()
    if c[0]>'end':
        s.append(0.0);
        q.append(int(c[4:]))
    elif c[0]<'d':
        s[-1]=s[-1]+1
    else:
        s.pop()*t.pop()+s[-1]
print(int(s[0])if s[0]<2**32 else'OVERFLOW!!!')


t = int(input())
a =[]
flag = 2
lim = 2**32
for i in range(t):
    a.append(input().split())
    if flag ==2 and a[i]==['add']:
        flag = 0
for i in range(a.index([add])):
    if
while ['end'] in a:
    if flag >=1:
        break

    f=0
    q = a.index(['end'])
    k = 1
    del a[q]
    while q-k >=0 and len(a[q-k])<=1:
        if a[q-k]==['add']:

            f+=1
            del a[q-k]
            k+=1
        elif a[q-k]!=['end']:
            f+=a[q-k][0]
            del a[q-k]
            k+=1
    if a[q-k][0]=='for':
        a[q-k][0] = int(a[q-k][1])*f
        if a[q-k][0]+1>lim:
            flag = 1
            break
        del a[q-k][1]
count = 0

for i in range(len(a)):
    if flag>=1:
        break
    if a[i][0]=='add':
        count+=1
    else:
        count+=a[i][0]
a[0][0]=count

if flag == 1 or a[0][0]+1>lim:
    print('OVERFLOW!!!')
elif flag == 2:
    print(0)
else:
    print(a[0][0])


lim = 2**32
t = int(input())
flag = 0
a = []
f = 0
summ = 0
for i in range(t):
    inp = input()
    if inp[0:3] == 'for':
        a.append(int(inp[4:]))
    if inp[0:3] == 'add':
        f = 1
        for s in a:
            f*=s
            if f>=lim:
                flag = 1
                i = t
                break
        if flag ==1:
            break
        summ+=f
        if summ >=lim:
            flag = 1
            break
    if inp[0:3] == 'end':
        a.pop()
if flag == 1:
    print("OVERFLOW!!!")
else:
    print(summ)


t = int(input())
a =[]
flag = 2
for i in range(t):
    a.append(input().split())
    if flag ==2 and a[i]==['add']:
        flag = 0
while ['end'] in a:
    if flag >=1:
        break

    f=0
    q = a.index(['end'])
    k = 1
    del a[q]
    while q-k >=0 and len(a[q-k])<=1:
        if a[q-k]==['add']:

            f+=1
            del a[q-k]
            k+=1
        elif a[q-k]!=['end']:
            f+=a[q-k][0]
            del a[q-k]
            k+=1
    if a[q-k][0]=='for':
        a[q-k][0] = int(a[q-k][1])*f
        if a[q-k][0]+1>2**32:
            flag = 1
            break
        del a[q-k][1]
count = 0

for i in range(len(a)):
    if flag>=1:
        break
    if a[i][0]=='add':
        count+=1
    else:
        count+=a[i][0]
a[0][0]=count

if flag == 1 or a[0][0]+1>2**32:
    print('OVERFLOW!!!')
elif flag == 2:
    print(0)
else:
    print(a[0][0])


t = int(input())
lim = 2**32
flag = 0
a = []
f = 0
sum = 0
for i in range(t):
    inp = input()
    if inp[0:3] == 'for':
        a.append(int(inp[4:]))
        f = 1
        for s in a:
            f*=s
            if f>=lim:
                flag = 1
                break
        if flag ==1:
            break
        sum+=f
        if flag == 1:
            break
    if inp[0:3] == 'end':
        del a[len(a)-1]
if flag == 1:
    print("OVERFLOW!!!")
else:
    print(sum)


t = int(input())
a =[]
for i in range(t):
    a.append(input().split())
while ['end'] in a:
    f=0
    q = a.index(['end'])
    k = 1
    del a[q]
    while q-k >=0 and len(a[q-k])<=1:
        if a[q-k]==['add']:

            f+=1
            del a[q-k]
            k+=1
        elif a[q-k]!=['end']:
            f+=a[q-k][0]
            del a[q-k]
            k+=1
    if a[q-k][0]=='for':
        a[q-k][0] = int(a[q-k][1])*f
        del a[q-k][1]
count = 0

for i in range(len(a)):
    if a[i][0]=='add':
        count+=1
    else:
        count+=a[i][0]
a[0][0]=count

if int(a[0][0])+1>2**32:
    print('OVERFLOW!!!')
else:
    print(a[0][0])


t = int(input())
lim = 2**32
flag = 0
a = []
f = 0
sum = 0
for i in range(t):
    inp = input()
    if inp[0:3] == 'for':
        a.append(int(inp[4:]))
    if inp[0:3] == 'add':
        f = 1
        for s in a:
            f*=s
            if f>=lim:
                flag = 1
                i = t
                break
        if flag ==1:
            break
        sum+=f
        if sum >=lim:
            flag = 1
            break
    if inp[0:3] == 'end':
        del a[len(a)-1]
if flag == 1:
    print("OVERFLOW!!!")
else:
    print(sum)


t = int(input())
a =[]
for i in range(t):
    a.append(input().split())
while ['end'] in a:
    f=0
    q = a.index(['end'])
    k = 1
    del a[q]
    while q-k >=0 and len(a[q-k])<=1:
        if a[q-k]==['add']:

            f+=1
            del a[q-k]
            k+=1
        elif a[q-k]!=['end']:
            f+=a[q-k][0]
            del a[q-k]
            k+=1
    if a[q-k][0]=='for':
        a[q-k][0] = int(a[q-k][1])*f
        del a[q-k][1]
count = 0
if len(a)>1:
    for i in range(len(a)):
        if a[i][0]=='add':
            count+=1
        else:
            count+=a[i][0]
    a[0][0]=count

if a[0][0]+1>2**32:
    print('OVERFLOW!!!')
else:
    print(a[0][0])


def gogo():
    cnt = 1
    while cnt != 0:
        t = input()
        if t == "end":
            cnt -= 1
        elif t == "add":
            return True
        else:
            cnt += 1
    return False

x = 0
l = int(input())
ml = []
m = 1
f = False
while l != 0:
    l -= 1
    c = input()
    if c == "end":
        m //= ml.pop()
    elif c == "add":
        x += m
        if x > 4294967295:
            f = True
            break
    else:
        tc = int(c.split()[1])
        m *= tc
        if m > 4294967295:
            if gogo():
                f = True
                break
            m //= tc
        ml.append(tc)
if f:
    print("OVERFLOW!!!")
else:
    print(int(x))


def gogo():
    cnt = 1
    i = 0
    while cnt != 0:
        i += 1
        t = input()
        if t[0] == "e":
            cnt -= 1
        elif t[0] == "a":
            return -1
        else:
            cnt += 1
    return i

x = 0
l = int(input())
ml = []
m = 1
f = False
while l != 0:
    l -= 1
    c = input()
    if c[0] == "e":
        m //= ml.pop()
    elif c[0] == "a":
        x += m
        print(ml)
        if x > 4294967295:
            f = True
            break
    else:
        tc = int(c.split()[1])
        m *= tc
        ml.append(tc)
        if m > 4294967295:
            gr = gogo()
            if gr == -1:
                f = True
                break
            l -= gr
            m //= tc
            ml.pop()
if f:
    print("OVERFLOW!!!")
else:
    print(int(x))


x = 0
l = int(input())
ml = []
m = 1
f = False
while l != 0:
    l -= 1
    c = input().split(" ")
    if len(c) == 2:
        m *= int(c[1])
        if m > 4294967295:
            f = True
            break
        ml.append(int(c[1]))
    elif c[0] == "end":
        m /= ml.pop()
    elif c[0] == "add":
        x += m
        if x > 4294967295:
            f = True
            break
if f:
    print("OVERFLOW!!!")
else:
    print(int(x))


def gogo():
    cnt = 1
    i = 0
    while cnt != 0:
        i += 1
        t = input()
        if t[0] == "e":
            cnt -= 1
        elif t[0] == "a":
            return -1
        else:
            cnt += 1
    return i

x = 0
l = int(input())
ml = []
m = 1
f = False
while l != 0:
    l -= 1
    c = input()
    if c[0] == "e":
        m //= ml.pop()
    elif c[0] == "a":
        x += m
        if x > 4294967295:
            f = True
            break
    else:
        tc = int(c.split()[1])
        m *= tc
        if m > 4294967295:
            gr = gogo()
            if gr == -1:
                f = True
                break
            l -= gr
            m //= tc
        ml.append(tc)
if f:
    print("OVERFLOW!!!")
else:
    print(int(x))


x = 0
l = int(input())
ml = []
m = 1
f = False
while l != 0:
    l -= 1
    c = input().split(" ")
    if len(c) == 2:
        m *= int(c[1])
        ml.append(int(c[1]))
    elif c[0] == "end":
        m /= ml.pop()
    elif c[0] == "add":
        x += m
        if x > 4294967295:
            f = True
            break
if f:
    print("OVERFLOW!!!")
else:
    print(int(x))


x = 0
l = int(input())
ml = []
m = 1
f = False
while l != 0:
    l -= 1
    c = input().split(" ")
    if len(c) == 2:
        m *= int(c[1])
        ml.append(int(c[1]))
    elif c[0] == "end":
        m //= ml.pop()
    elif c[0] == "add":
        if m > 4294967295:
            f = True
            break
        x += m
        if x > 4294967295:
            f = True
            break
if f:
    print("OVERFLOW!!!")
else:
    print(int(x))


def gogo():
    cnt = 1
    i = 0
    while cnt != 0:
        i += 1
        t = input()
        if t[0] == "e":
            cnt -= 1
        elif t[0] == "a":
            return -1
        else:
            cnt += 1
    return i

x = 0
l = int(input())
ml = []
m = 1
f = False
while l != 0:
    l -= 1
    c = input()
    if c[0] == "e":
        m //= ml.pop()
    elif c[0] == "a":
        x += m
        if x > 4294967295:
            f = True
            break
    else:
        tc = int(c.split()[1])
        m *= tc
        ml.append(tc)
        if m > 4294967295:
            gr = gogo()
            if gr == -1:
                f = True
                break
            l -= gr
            m //= tc
            ml.pop()
if f:
    print("OVERFLOW!!!")
else:
    print(int(x))


def gogo():
    cnt = 1
    while cnt != 0:
        t = input()
        if t == "end":
            cnt -= 1
        elif t == "add":
            return True
        else:
            cnt += 1
    return False

x = 0
l = int(input())
ml = []
m = 1
f = False
while l != 0:
    l -= 1
    c = input().split(" ")
    if len(c) == 2:
        m *= int(c[1])
        if m > 4294967295:
            if gogo():
                f = True
                break
            m //= int(c[1])
        ml.append(int(c[1]))
    elif c[0] == "end":
        m //= ml.pop()
    elif c[0] == "add":
        x += m
        if x > 4294967295:
            f = True
            break
if f:
    print("OVERFLOW!!!")
else:
    print(int(x))


x = 0
l = int(input())
ml = []
m = 1
f = False
while l != 0:
    l -= 1
    c = input().split(" ")
    if len(c) == 2:
        m *= int(c[1])
        ml.append(int(c[1]))
    elif c[0] == "end":
        m /= ml.pop()
    elif c[0] == "add":
        if m > 4294967295:
            f = True
            break
        x += m
        if x > 4294967295:
            f = True
            break
if f:
    print("OVERFLOW!!!")
else:
    print(int(x))


def f(R):
	if R==0:
		return 0
	F=0;S=0;cur=1
	for i in range(150):
		t=min(cur,R-F-S)
		#print(str(i)+' '+str(F)+' '+str(S)+' '+str(t))
		if i%2==0:
			F=F+t
		else:
			S=S+t
		cur=cur*2
		if R==F+S:
			break
	ans=F*F+S*S+S
	return ans

#L,R = map(int,input().strip().split())
#L = L-1
#ans = f(R)-f(L)
#ans = ans%1000000007
#print(ans)

N=int(input())
V=[]
it=1
flag=0
x=0
for i in range(N):
    s=input()
    s+=" last"
    s=s.strip().split()
    if s[0]=="for":
        n=int(s[1].strip())
        it*=n
        V.append(n)
    elif s[0]=="add":
        x+=it
        if x>=2**32:
            flag=1
    elif s[0]=="end":
        if len(V)>1:
            it/=V[-1]
            V.pop()
    #print(str(s)+" "+str(x)+" "+str(it)+" "+str(V))
if flag==1:
    print("OVERFLOW!!!")
else:
    print(int(x))
##t=[1]
##while t[len(t)-1]*2<=n:
##	t.append(t[-1]*2)
##x=0
##i=0
##while i<len(t) and x+t[i]<=n:
##	x=x+t[i]
##	i=i+1
##
##if n&(n+1)==0:
##	print n
##elif k==1:
##	print n
##else:
##	print 2*t[-1]-1