#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include<set>
#include<string>
using namespace std;
int main(){
	string x;
	cin>>x;
	if(x.find("181")!=-1|| x.find("272")!=-1 || x.find("363")!=-1 || x.find("454")!=-1 || x.find("545")!=-1 || x.find("636")!=-1 || x.find("727")!=-1 || x.find("818")!=-1){
		cout<<2<<endl; return 0;
	}
	cout<<1<<endl;

	return 0;
}


#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<algorithm>
using namespace std;
int main(){
	string x;
	cin>>x;
	int counter=0,answer=1;
	for(int i=0;i<x.size();i++){
		if((x[i]-'0')+(x[i+1]-'0')==9){
			counter++;
		}else{
			if(!(counter%2) && counter>1){
				answer*=(counter)/2+1;
			}
			counter=0;
		}

	}
	cout<<answer<<endl;

	return 0;
}


#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
#define ll                  long long
#define pb                push_back
#define fr(i,n)           for (ll i=0;i<n;i++)
#define fr1(i,n)          for(ll i=1;i<=n;i++)

int main()
{

    ll m,n,i,j,ans=1,x,y;
    string s,s1;
    cin>>s;
    ll cnt=0;
    fr(i,s.size()-1)
    {
        x=s[i]-'0';
        y=s[i+1]-'0';
        if(x+y==9)  cnt++;
        else
        {
            if(cnt>0 && cnt%2==0) ans*=((cnt+2)/2);cnt=0;
        }
    }

    if(cnt>0 && cnt%2==0) ans*=((cnt+2)/2);

    cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    string num, s1 = "", s2 = "";
    int i, len, count1 = 0, count2 = 0;
    cin >> num;
    len = num.length();
    i = len-1;
    while (i >= 0)
    {
        if (num[i] == '9')
        {
            s1 = num[i] + s1;
            count1++;
            i--;
        }
        else if (i == 0)
        {
            s2= num[i] + s2;
            i--;
        }
        else if ((num[i] - '0') + (num[i-1] - '0') == 9)
        {
            s1 = '9' +s1;
            count1++;
            i -= 2;
        }
        else
        {
            s1 = num[i] + s1;
            i--;
        }
    }

    i = len-2;
    s2 += num[len-1];
    while (i > 0)
    {
        if (num[i] == '9')
        {
            s2 = num[i] + s2;
            count2++;
            i--;
        }
        else if (i == 0)
        {
            s2= num[i] + s2;
            i--;
        }
        else if ((num[i] - '0') + (num[i-1] - '0') == 9)
        {
            s2 = '9' + s2;
            count2++;
            i -= 2;
        }
        else
        {
            s2 = num[i] + s2;
            i--;
        }
    }
    if ((count1 == 0 && count2 == 0) || count1 != count2)
        cout<<"1";
    else if (count1 == count2)
    {
        if (s1 != s2)
            cout<<"2";
        else
            cout<<"1";
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    string num, s1 = "", s2 = "";
    int i, len, count1 = 0, count2 = 0;
    cin >> num;
    len = num.length();
    i = len-1;
    while (i >= 0)
    {
        if (num[i] == '9')
        {
            s1 = num[i] + s1;
            count1++;
            i--;
        }
        else if (i == 0)
        {
            s1= num[i] + s1;
            i--;
        }
        else if ((num[i] - '0') + (num[i-1] - '0') == 9)
        {
            s1 = '9' +s1;
            count1++;
            i -= 2;
        }
        else
        {
            s1 = num[i] + s1;
            i--;
        }
    }

    i = len-2;
    s2 += num[len-1];
    while (i >= 0)
    {
        if (num[i] == '9')
        {
            s2 = num[i] + s2;
            count2++;
            i--;
        }
        else if (i == 0)
        {
            s2= num[i] + s2;
            i--;
        }
        else if ((num[i] - '0') + (num[i-1] - '0') == 9)
        {
            s2 = '9' + s2;
            count2++;
            i -= 2;
        }
        else
        {
            s2 = num[i] + s2;
            i--;
        }
    }
    if ((count1 == 0 && count2 == 0) || count1 != count2)
        cout<<"1";
    else if (count1 == count2)
    {
        if (s1 != s2)
            cout<<"2";
        else
            cout<<"1";
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    string num, s1 = "", s2 = "";
    int i, len, count1 = 0, count2 = 0;
    cin >> num;
    len = num.length();
    i = len-1;
    while (i > 0)
    {
        if (num[i] == '9')
        {
            s1 = num[i] + s1;
            count1++;
            i--;
        }
        else if ((num[i] - '0') + (num[i-1] - '0') == 9)
        {
            s1 = '9' +s1;
            count1++;
            i -= 2;
        }
        else
        {
            s1 = num[i] + s1;
            i--;
        }
    }

    i = len-2;
    s2 += num[len-1];
    while (i > 0)
    {
        if (num[i] == '9')
        {
            s2 = num[i] + s2;
            count2++;
            i--;
        }
        else if ((num[i] - '0') + (num[i-1] - '0') == 9)
        {
            s2 = '9' + s2;
            count2++;
            i -= 2;
        }
        else
        {
            s2 = num[i] + s2;
            i--;
        }
    }
    if ((count1 == 0 && count2 == 0) || count1 != count2)
        cout<<"1";
    else if (count1 == count2)
    {
        if (s1 != s2)
            cout<<"2";
        else
            cout<<"1";
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <fstream>
#include <set>
#include <map>
#include <iomanip>
#include <string>
#include <cstring>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <complex>
#include <list>
#include <climits>
#include <cctype>
#include <bitset>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<long long> vll;
#define RIP(i, a, b) \
for (int i = int(a); i < int(b); i++)
#define MOD 1000007
#define PI 3.1415926536
#define NIL -1
#define MAX 100001
#define INF numeric_limits<double>::infinity();
#define ll  unsigned long long

map<int,int>p;
int G[101][101] , V , b = 0;
vector<int> visted(100),cycle;
stack <int> s ,s1;
int cnt = 0;

void containsOdd(int src)
{
    int colorArr[V+1];
    for (int i = 1; i <= V; ++i)
        colorArr[i] = -1;

    // Assign first color to source
    colorArr[src] = 1;

    // Create a queue (FIFO) of vertex numbers and
    // enqueue source vertex for BFS traversal
    queue <int> q;
    q.push(src);

    // Run while there are vertices in queue (Similar to BFS)
    while (!q.empty())
    {
        // Dequeue a vertex from queue
        int u = q.front();
        q.pop();
        //cout<<u<<endl;
        // Find all non-colored adjacent vertices
        for (int v = 1; v <= V; v++)
        {
            // An edge from u to v exists and destination
            // v is not colored
            if ((G[u][v] || G[v][u]) && colorArr[v] == -1)
            {
                // Assign alternate color to this adjacent
                // v of u
                colorArr[v] = 1 - colorArr[u];
                q.push(v);
                //cout<<u<<"  "<<v<<endl;
            }

            // An edge from u to v exists and destination
            // v is colored with same color as u
            else if ((G[u][v] || G[v][u]) && colorArr[v] == colorArr[u]){
                cnt++;
                //cout<<u<<"  "<<v<<endl;
            }
        }
        if(q.empty()){
            for (int v = 1; v <= V; v++){
                if(colorArr[v] == -1){
                    colorArr[v] = 1;
                    q.push(v);
                    break;
                }
            }
        }
    }
}

int nChoosek( int n, int k )
{
    if (k > n) return 0;
    if (k * 2 > n) k = n-k;
    if (k == 0) return 1;

    int result = n;
    for( int i = 2; i <= k; ++i ) {
        result *= (n-i+1);
        result /= i;
    }
    return result;
}

int main()
{
    string s;
    cin>>s;
    s+='*';
    int ind = 0 , c = 0 , ret = 1;
    vector<int> v;
    for(int i = 0 ; i < s.size() ; i++){
        if((s[i]-'0' + s[i+1]-'0') == 9){
            //cout<<s[i]<<"  "<<s[i+1]<<endl;
            c++;
        }
        else{
            if(c > 1){
                //cout<<">  "<<c<<endl;
                v.push_back(c);
                c = 0;
            }
            else if(c == 1){
                //cout<<">>  "<<c<<endl;
                //ind = 1;
                c = 0;
            }
        }
    }
    for(int i = 0 ; i < v.size();i++){
        if(v[i]%2 == 0){
            ret*=((v[i]+2)/2);
        }
    }
    if(ret > 0)
        cout<<ret<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <fstream>
#include <set>
#include <map>
#include <iomanip>
#include <string>
#include <cstring>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <complex>
#include <list>
#include <climits>
#include <cctype>
#include <bitset>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<long long> vll;
#define RIP(i, a, b) \
for (int i = int(a); i < int(b); i++)
#define MOD 1000007
#define PI 3.1415926536
#define NIL -1
#define MAX 100001
#define INF numeric_limits<double>::infinity();
#define ll  unsigned long long

map<int,int>p;
int G[101][101] , V , b = 0;
vector<int> visted(100),cycle;
stack <int> s ,s1;
int cnt = 0;

void containsOdd(int src)
{
    int colorArr[V+1];
    for (int i = 1; i <= V; ++i)
        colorArr[i] = -1;

    // Assign first color to source
    colorArr[src] = 1;

    // Create a queue (FIFO) of vertex numbers and
    // enqueue source vertex for BFS traversal
    queue <int> q;
    q.push(src);

    // Run while there are vertices in queue (Similar to BFS)
    while (!q.empty())
    {
        // Dequeue a vertex from queue
        int u = q.front();
        q.pop();
        //cout<<u<<endl;
        // Find all non-colored adjacent vertices
        for (int v = 1; v <= V; v++)
        {
            // An edge from u to v exists and destination
            // v is not colored
            if ((G[u][v] || G[v][u]) && colorArr[v] == -1)
            {
                // Assign alternate color to this adjacent
                // v of u
                colorArr[v] = 1 - colorArr[u];
                q.push(v);
                //cout<<u<<"  "<<v<<endl;
            }

            // An edge from u to v exists and destination
            // v is colored with same color as u
            else if ((G[u][v] || G[v][u]) && colorArr[v] == colorArr[u]){
                cnt++;
                //cout<<u<<"  "<<v<<endl;
            }
        }
        if(q.empty()){
            for (int v = 1; v <= V; v++){
                if(colorArr[v] == -1){
                    colorArr[v] = 1;
                    q.push(v);
                    break;
                }
            }
        }
    }
}

int main()
{
    string s;
    cin>>s;
    s+='*';
    int ind = 0 , c = 0 , ret = 0;
    vector<int> v;
    for(int i = 0 ; i < s.size() ; i++){
        if((s[i]-'0' + s[i+1]-'0') == 9){
            c++;
        }
        else{
            if(c > 1){
                //cout<<">  "<<c<<endl;
                v.push_back(c);
                c = 0;
            }
            else if(c == 1){
                //cout<<">>  "<<c<<endl;
                //ind = 1;
                c = 0;
            }
        }
    }
    for(int i = 0 ; i < v.size();i++){
        if(v[i]%2 == 0)
            ret+=2;
        else
            ret+=1;
    }
    if(ret > 0)
        cout<<ret<<endl;
    else
        cout<<1<<endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <fstream>
#include <set>
#include <map>
#include <iomanip>
#include <string>
#include <cstring>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <complex>
#include <list>
#include <climits>
#include <cctype>
#include <bitset>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<long long> vll;
#define RIP(i, a, b) \
for (int i = int(a); i < int(b); i++)
#define MOD 1000007
#define PI 3.1415926536
#define NIL -1
#define MAX 100001
#define INF numeric_limits<double>::infinity();
#define ll  unsigned long long

map<int,int>p;
int G[101][101] , V , b = 0;
vector<int> visted(100),cycle;
stack <int> s ,s1;
int cnt = 0;

void containsOdd(int src)
{
    int colorArr[V+1];
    for (int i = 1; i <= V; ++i)
        colorArr[i] = -1;

    // Assign first color to source
    colorArr[src] = 1;

    // Create a queue (FIFO) of vertex numbers and
    // enqueue source vertex for BFS traversal
    queue <int> q;
    q.push(src);

    // Run while there are vertices in queue (Similar to BFS)
    while (!q.empty())
    {
        // Dequeue a vertex from queue
        int u = q.front();
        q.pop();
        //cout<<u<<endl;
        // Find all non-colored adjacent vertices
        for (int v = 1; v <= V; v++)
        {
            // An edge from u to v exists and destination
            // v is not colored
            if ((G[u][v] || G[v][u]) && colorArr[v] == -1)
            {
                // Assign alternate color to this adjacent
                // v of u
                colorArr[v] = 1 - colorArr[u];
                q.push(v);
                //cout<<u<<"  "<<v<<endl;
            }

            // An edge from u to v exists and destination
            // v is colored with same color as u
            else if ((G[u][v] || G[v][u]) && colorArr[v] == colorArr[u]){
                cnt++;
                //cout<<u<<"  "<<v<<endl;
            }
        }
        if(q.empty()){
            for (int v = 1; v <= V; v++){
                if(colorArr[v] == -1){
                    colorArr[v] = 1;
                    q.push(v);
                    break;
                }
            }
        }
    }
}

int nChoosek( int n, int k )
{
    if (k > n) return 0;
    if (k * 2 > n) k = n-k;
    if (k == 0) return 1;

    int result = n;
    for( int i = 2; i <= k; ++i ) {
        result *= (n-i+1);
        result /= i;
    }
    return result;
}

int main()
{
    string s;
    cin>>s;
    s+='*';
    unsigned long long ind = 0 , c = 0 , ret = 1;
    vector<unsigned long long> v;
    for(int i = 0 ; i < s.size() ; i++){
        if((s[i]-'0' + s[i+1]-'0') == 9){
            //cout<<s[i]<<"  "<<s[i+1]<<endl;
            c++;
        }
        else{
            if(c > 1){
                //cout<<">  "<<c<<endl;
                v.push_back(c);
                c = 0;
            }
            else if(c == 1){
                //cout<<">>  "<<c<<endl;
                //ind = 1;
                c = 0;
            }
        }
    }
    for(int i = 0 ; i < v.size();i++){
        if(v[i]%2 == 0){
            ret*=((v[i]+2)/2);
        }
    }
    if(ret > 0)
        cout<<ret<<endl;
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstring>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <set>
#include <bitset>
#include <fstream>

using namespace std;

#define V vector
#define se second
#define fr first
#define all(v)  ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define clr(v, d) memset(v, d, sizeof(v))
#define sf(x) scanf("%d", &x);
#define sf2(x, y) scanf("%d %d", &x, &y);
#define sf3(x, y, z) scanf("%d %d %d", &x, &y, &z);
#define sfll(x) scanf("%I64d", &x);
#define sfll2(x, y) scanf("%I64d %I64d", &x, &y);
#define sfll3(x, y, z) scanf("%I64d %I64d %I64d", &x, &y, &z);
int dx[]{1, -1, 0, 0, 1, -1, 1, -1};
int dy[]{0, 0, 1, -1, 1, -1, -1, 1};
typedef unsigned long long          ull;
typedef long long                   ll;
typedef long double                 LD;

int main()
{
	string s;
	while (cin >> s){
		ll ans = 1, tmp = 0;
		for (int i = 0; i < int(s.size() - 1) ; i++){
			if (s[i] + s[i + 1] - 2 * '0' == 9) tmp++;
			else if (tmp != 0) {
				if (tmp % 2 == 0)
					ans *= (tmp);
				tmp = 0;
			}
		}
		if (tmp != 0 && tmp % 2 == 0) ans *= (tmp);
		cout << ans << endl;

	}

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>

using namespace std;

#define V vector
#define MP make_pair
#define MAX INT_MAX
#define MIN INT_MIN
#define all(v)  ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define clr(v, d) memset(v, d, sizeof(v))
#define sf(x) scanf("%d", &x)
#define sf2(x, y) scanf("%d %d", &x, &y)
#define sf3(x, y, z) scanf("%d %d %d", &x, &y, &z)
#define sfll(x) scanf("%I64d", &x)
#define sfll2(x, y) scanf("%I64d %I64d", &x, &y)
#define sfll3(x, y, z) scanf("%I64d %I64d %I64d", &x, &y, &z)
//ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
int dx[]{1, -1, 0, 0, 1, -1, 1, -1};
int dy[]{0, 0, 1, -1, 1, -1, -1, 1};
typedef unsigned long long          ull;
typedef long long                   ll;
typedef long double                 LD;
const ll size = 1000006, oo = 1e13, mod = 1e9 + 7;
ll gcd(ll a, ll b){
	if (b == 0) return a;
	gcd(b, a % b);
}
double dis(ll a, ll b, ll c, ll d){
	return sqrt((a - c) * (a - c) + (b - d) * (b - d));
}

//int n, m, ii, jj, a, b;
//
//ll go(int i, int j , int d1 , int d2){
//	if (i < 0 || i >= n || j < 0 || j >= m) return MAX;
//	if (d1 == i && d2 == j) return 0;
//	ll ret = MAX;
//	for (int k = 0; k < 4; k++){
//		ret = min(ret , go(i + a * dx[k], j + b * dy[k] , d1 , d2) + 1);
//	}
//	return ret;
//}
//

int main()
{
	string s;
	while (cin >> s){
		ull ans = 1;
		ll sum = 0;
		for (int i = 1; i < s.size(); i++){
			if ((s[i] - '0') + (s[i - 1] - '0') == 9 && s[i] != '0' && s[i - 1] != '0')
				sum++;
			else if(sum != 0 ){
				sum++;
				if (sum % 2 == 1){
					ans *= ull(ceil(sum / 2.0) );
				}
				sum = 0;
			}
		}
		if (sum != 0){
			sum++;
			if (sum % 2 == 1) {
				ans *= ull(ceil(sum / 2.0));
			}
		}
		cout << ans << endl;
	}

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstring>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <set>
#include <bitset>
#include <fstream>

using namespace std;

#define V vector
#define se second
#define fr first
#define all(v)  ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define clr(v, d) memset(v, d, sizeof(v))
#define sf(x) scanf("%d", &x);
#define sf2(x, y) scanf("%d %d", &x, &y);
#define sf3(x, y, z) scanf("%d %d %d", &x, &y, &z);
#define sfll(x) scanf("%I64d", &x);
#define sfll2(x, y) scanf("%I64d %I64d", &x, &y);
#define sfll3(x, y, z) scanf("%I64d %I64d %I64d", &x, &y, &z);
int dx[]{1, -1, 0, 0, 1, -1, 1, -1};
int dy[]{0, 0, 1, -1, 1, -1, -1, 1};
typedef unsigned long long          ull;
typedef long long                   ll;
typedef long double                 LD;

int main()
{
	string s;
	while (cin >> s){
		ll ans = 1, tmp = 0;
		for (int i = 0; i < int(s.size() - 1) ; i++){
			if (s[i] + s[i + 1] - 2 * '0' == 9) tmp++;
			else if (tmp != 0) {
				if (tmp % 2 == 0)
					ans *= (tmp / 2 + 1);
				tmp = 0;
			}
		}
		if (tmp != 0 && tmp % 2 == 0) ans *= (tmp / 2 + 1);
		cout << ans << endl;

	}

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstring>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <set>
#include <bitset>
#include <fstream>

using namespace std;

#define V vector
#define se second
#define fr first
#define all(v)  ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define clr(v, d) memset(v, d, sizeof(v))
#define sf(x) scanf("%d", &x);
#define sf2(x, y) scanf("%d %d", &x, &y);
#define sf3(x, y, z) scanf("%d %d %d", &x, &y, &z);
#define sfll(x) scanf("%I64d", &x);
#define sfll2(x, y) scanf("%I64d %I64d", &x, &y);
#define sfll3(x, y, z) scanf("%I64d %I64d %I64d", &x, &y, &z);
int dx[]{1, -1, 0, 0, 1, -1, 1, -1};
int dy[]{0, 0, 1, -1, 1, -1, -1, 1};
typedef unsigned long long          ull;
typedef long long                   ll;
typedef long double                 LD;

int main()
{
	string s;
	while (cin >> s){
		ll ans = 1, tmp = 0;
		for (int i = 0; i < int(s.size() - 1) ; i++){
			if (s[i] + s[i + 1] - 2 * '0' == 9) tmp++;
			else if (tmp != 0) {
				if (tmp % 2 == 0)
					ans *= (tmp / 2 + 1);
				tmp = 0;
			}
		}
		if (tmp != 0 && tmp % 2 == 0) ans *= (tmp);
		cout << ans << endl;

	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 20;
ll A[maxn];
char s[maxn];
int main(void)
{
	scanf("%s",s+1);
	ll sum = 1,ans= 0;
	int len =strlen(s + 1);
	A[1] = 1;
	for(int i = 2;i<=maxn;i++)
	{
		if(i&1)A[i] = A[i-1] + 1;
		else	A[i] = A[i-1] ;
	}
	for(int i = 1;i <= len; i ++)
	{
		if((s[i-1] + s[i] - 2*'0')==9)
		{
			ans++;
		}
		else
		{
			if(ans >= 2)
			{
				sum *= A[ans];
			}
			ans = 1;
		}
	}
	if(ans >= 2)
			{
				sum *= A[ans];
			}
	cout<<sum<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 20;
ll A[maxn];
char s[maxn];
int main(void)
{
	scanf("%s",s+1);
	ll sum = 1,ans= 1;
	int len =strlen(s + 1);
	A[1] = 1;
	for(int i = 2;i<=maxn;i++)
	{
		if(i&1)A[i] = A[i-1] + 1;
		else	A[i] = A[i-1] ;
	}
	for(int i = 1;i <= len+1; i ++)
	{
		if((s[i-1] + s[i] - 2*'0')==9)
		{
			ans++;
		}
		else
		{
			if(ans&1&&ans>1)
			{
				sum *= ans/2 + 1;
			}
			ans = 1;
		}
	}
	cout<<sum<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 20;
ll A[maxn];
char s[maxn];
int main(void)
{
	scanf("%s",s+1);
	ll sum = 0,ans= 1;
	int len =strlen(s + 1);
	A[1] = 1;
	for(int i = 2;i<=maxn;i++)
	{
		if(i&1)A[i] = A[i-1] + 1;
		else	A[i] = A[i-1] ;
	}
	for(int i = 1;i <= len+1; i ++)
	{
		if((s[i-1] + s[i] - 2*'0')==9)
		{
			ans++;
		}
		else
		{
			if(ans&1&&ans>1)
			{
				sum += ans/2 + 1;
			}
			ans = 1;
		}
	}
	cout<<sum<<endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    string s;
    cin>>s;
    ll z=s.size();
    ll cnt=0;
    for(int i=0;i<z-1;i++)
    {
        if(s[i]-'0'+s[i+1]-'0'==9)
        {
            if(i<z-1)
            {
                if(s[i+1]-'0'+s[i+2]-'0'==9)
                {  if(i<z-2)
                {
                    if(s[i+2]-'0'+s[i+3]-'0'!=9)
                    {cnt++;

                    }
                }
                    i++;
                }
            }
        }
    }
    cout<<cnt;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{
    string s;
    cin>>s;
    ll z=s.size();
    ll cnt=0;
    for(int i=0;i<z-1;i++)
    {
        if(s[i]-'0'+s[i+1]-'0'==9)
        {
            if(i<z-1)
            {
                if(s[i+1]-'0'+s[i+2]-'0'==9)
                {  if(i<z-2)
                {
                    if(s[i+2]-'0'+s[i+3]-'0'!=9)
                    {cnt+=2;

                    }
                }
                    i++;
                }
            }
        }
    }
    if(!cnt)cout<<"1";
    else{
    cout<<cnt;}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll pows(ll a,ll b)
{
    if(b==0)
    {
        return 1;
    }
    if(b%2==0)
    {
        return pows(a*a,b/2);
    }
    if(b%2==1)
    {
        return a*pows(a*a,b/2);
    }
}
int main()
{
    string s;
    cin>>s;
    ll z=s.size();
    ll cnt=1;
    ll ans=1;
    for(ll i=0;i<z-1;i++)
    {
        if(s[i]-'0'+s[i+1]-'0'==9)
        {
            cnt++;
        }
        else
        {
            if(cnt%2==1&&cnt!=1)
            {ans*=(cnt+1)/2;

            }
            cnt=1;
        }
    }
    if(cnt%2==1&&cnt!=1)
    {
        ans*=(cnt+1)/2;
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll pows(ll a,ll b)
{
    if(b==0)
    {
        return 1;
    }
    if(b%2==0)
    {
        return pows(a*a,b/2);
    }
    if(b%2==1)
    {
        return a*pows(a*a,b/2);
    }
}
int main()
{
    string s;
    cin>>s;
    ll z=s.size();
    ll cnt=0;
    for(int i=0;i<z-1;i++)
    {
        if(s[i]-'0'+s[i+1]-'0'==9)
        {
            if(i<z-1)
            {
                if(s[i+1]-'0'+s[i+2]-'0'==9)
                {  if(i<z-2)
                {
                    if(s[i+2]-'0'+s[i+3]-'0'!=9)
                    {cnt+=1;

                    }
                }
                    i++;
                }
            }
        }
    }
    if(!cnt)cout<<"1";
    else{
    ll ans=pows(2,cnt);
        cout<<ans;
    }
}


#include <bits/stdc++.h>
using namespace std;

char s[100001];
long long change(char s[])
{
    long long ans = 1;
    long long num = 0;
    for(int i = 1;i<strlen(s)-1;i++)
    {
        if(s[i-1]+s[i]-2*'0'== 9 && s[i]+s[i+1]-2*'0'== 9)
        {
            num++;
        }
        else if(num%2==1)
        {
            ans = ans*((num)*2);
            num = 0;
        }
        else
        {
            num = 0;
        }
    }
    if(num%2==1)
    {
        ans = (ans*(num*2));
    }
    return ans;
}

int main()
{
    scanf("%s",s);
    long long ans = change(s);

    printf("%lld\n",ans);
    return 0;
}


#include <iostream>
#include <string>
using namespace std;
string s;
long long judge(string s)
{
    long long ans = 1;
    long long num = 0;
    for(int i = 1;i<s.size()-1;i++)
    {
        if(s[i-1]+s[i]-2*'0'==9&&s[i]+s[i+1]-2*'0'==9)
        {
            num++;
        }
        else if(num%2==1)
        {
            ans = ans*((num+3)/2);
            //cout << "num " << num << endl;
            num = 0;
        }
        else
        {
            num = 0;
        }
    }
    if(num%2==1)
    {
        ans = (ans*(num+3)/2);
    }
    return ans;
}
int main()
{
    cin >> s;
    long long ans = judge(s);
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

char s[100001];
long long judge(char s[])
{
    long long ans = 1;
    long long num = 0;
    for(int i = 1;i<strlen(s)-1;i++)
    {
        if(s[i-1]+s[i]-2*'0'== 9 && s[i]+s[i+1]-2*'0'== 9)
        {
            num++;
        }
        else if(num%2==1)
        {
            ans = ans*((num+3)/2);
            num = 0;
        }
        else
        {
            num = 0;
        }
    }
    if(num%2==1)
    {
        ans = (ans*(num+3)/2);
    }
    return ans;
}

int main()
{
    scanf("%s",s);
    long long ans = judge(s);

    printf("%lld\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<double,double> dd;
typedef pair<ll,ll> pll;
typedef pair<string,int> psi;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<int> > vvi;
typedef vector<vector<ll> > vvll;
typedef vector<vector<ii> > vvip;
typedef vector<ii> vii;
#define pb push_back
#define F first
#define S second
#define MOD 1000000007
#define NA -1
#define ALL(v) v.begin(),v.end()
#define mp make_pair
#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)
#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define db(x) {cout << #x << " = " << (x) << endl;}
#define dba(a, x, y) {cout<<#a<<" :";FOR(i123,x,y)cout<<setw(4)<<(a)[i123];cout<<endl;}
#define clr(x) memset(x,0,sizeof(x));
#define sz(x) int((x).size())
#define endl '\n'
const int INF = INT_MAX;
const ll INFL = LLONG_MAX;
const ld pi = acos(-1);

typedef pair<pll,ll> ppll;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
#define ret return
ll ab(ll x)
{
	return x<0?-x:x;
}

int main()
{
	// freopen("input.txt","r",stdin);
	// freopen("output.txt","w",stdout);
	ios_base::sync_with_stdio(0); cout.precision(15); cout << fixed; cout.tie(0); cin.tie(0);
	string s;
	cin>>s;
	ll ct=0,ans=1LL;
	int n=s.length();
	for(int i=1;i<n;i++)
		if(s[i]-'0'+ s[i-1]-'0' == 9)
			ct++;
		else
		{
			ans*= (ct+2)/2;
			ct=0;
		}
	ans*=(ct+2)/2;
	cout<<ans<<endl;
	ret 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<double,double> dd;
typedef pair<ll,ll> pll;
typedef pair<string,int> psi;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<int> > vvi;
typedef vector<vector<ll> > vvll;
typedef vector<vector<ii> > vvip;
typedef vector<ii> vii;
#define pb push_back
#define F first
#define S second
#define MOD 1000000007
#define NA -1
#define ALL(v) v.begin(),v.end()
#define mp make_pair
#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)
#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define db(x) {cout << #x << " = " << (x) << endl;}
#define dba(a, x, y) {cout<<#a<<" :";FOR(i123,x,y)cout<<setw(4)<<(a)[i123];cout<<endl;}
#define clr(x) memset(x,0,sizeof(x));
#define sz(x) int((x).size())
#define endl '\n'
const int INF = INT_MAX;
const ll INFL = LLONG_MAX;
const ld pi = acos(-1);

typedef pair<pll,ll> ppll;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
#define ret return
ll ab(ll x)
{
	return x<0?-x:x;
}

int main()
{
	// freopen("input.txt","r",stdin);
	// freopen("output.txt","w",stdout);
	ios_base::sync_with_stdio(0); cout.precision(15); cout << fixed; cout.tie(0); cin.tie(0);
	string s;
	cin>>s;
	ll ct=0,ans=1LL;
	int n=s.length();
	for(int i=1;i<n;i++)
		if(s[i]-'0'+ s[i-1]-'0' == 9)
			ct++;
		else
		{
			if(ct%2 == 0)
			ans*= (ct+2)/2;
			ct=0;
		}
	ans*=(ct+2)/2;
	cout<<ans<<endl;
	ret 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<double,double> dd;
typedef pair<ll,ll> pll;
typedef pair<string,int> psi;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector<int> > vvi;
typedef vector<vector<ll> > vvll;
typedef vector<vector<ii> > vvip;
typedef vector<ii> vii;
#define pb push_back
#define F first
#define S second
#define MOD 1000000007
#define NA -1
#define ALL(v) v.begin(),v.end()
#define mp make_pair
#define rep(i, n) for (int (i) = 0; (i) < (n); (i) ++)
#define rep1(i, n) for (int (i) = 1; (i) <= (n); (i) ++)
#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define db(x) {cout << #x << " = " << (x) << endl;}
#define dba(a, x, y) {cout<<#a<<" :";FOR(i123,x,y)cout<<setw(4)<<(a)[i123];cout<<endl;}
#define clr(x) memset(x,0,sizeof(x));
#define sz(x) int((x).size())
#define endl '\n'
const int INF = INT_MAX;
const ll INFL = LLONG_MAX;
const ld pi = acos(-1);

typedef pair<pll,ll> ppll;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
#define ret return
ll ab(ll x)
{
	return x<0?-x:x;
}

int main()
{
	// freopen("input.txt","r",stdin);
	// freopen("output.txt","w",stdout);
	ios_base::sync_with_stdio(0); cout.precision(15); cout << fixed; cout.tie(0); cin.tie(0);
	string s;
	cin>>s;
	ll ct=0,ans=1LL;
	int n=s.length();
	for(int i=1;i<n;i++)
		if(s[i]-'0'+ s[i-1]-'0' == 9)
			ct++;
		else
		{
			if(ct%2 == 0)
			ans*= (ct+2)/2;
			ct=0;
		}
	if(ct%2 ==0)
	ans*=(ct+2)/2;
	cout<<ans<<endl;
	ret 0;
}


#include <iostream>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <map>
#include <unordered_map>
#include <stack>
#include <algorithm>
#include <stdio.h>
#include <cstring>					//  For memset
#include <sstream>

using namespace std;

#define lop(i,n) for(int i = 0 ; i < n ;i++)
#define VI	vector<int>
#define VLL	vector<ll>
#define VPI	vector<pair<int , int>>
#define VII	vector<VI>
#define all(v)	v.begin() , v.end()
#define SORT(v)	sort(all(v))
#define ll	long long
#define ull unsigned long long
#define scni(x)	scanf("%d",&x)				//  int
#define scnll(x)	scanf("%lld",&x)			//  ll
#define scnd(x)	scanf("%lf",&x)			//  double
#define prnti(x)	printf("%d\n",x)				//  int
#define prntll(x)	printf("%lld",x)				//  int
#define prntd(x)	printf("%.9f",x)				//  int
#define sz(x)  x.size()
#define SPC  <<" "<<
#define INF int(1e9)
#define pp push_back
#define P pair<int , int>
#define mp make_pair

const double EPS = 1e-9;
const double PI = 3.14159265359;

//  O( n * log(log(n)) )
vector<ll> primes;
void GeneratingPrimes(int top){
	vector<bool> p (top+1 , 1);
//	vector<ll> primes;
	p[0] = p[1] = 0;

	for(int i = 2; i <= sqrt(top) ; i++){
		if(p[i]){
			for(int j = i*i ; j <= top ; j+=i){
				p[j]=0;
			}
		}
	}
	lop(i , top+1)
		if(p[i])primes.push_back(i);
//	return primes;
}

ll gcd(ll a ,ll b){return (b==0)?a : gcd(b , a%b);}
ll lcm(ll a, ll b){return (a * b) / gcd(a , b);}

void EXTgcd(ll a , ll b , int & x0 , int & y0){
	if(b==0){ x0 =1 , y0 = 0; return; }
	EXTgcd( b , a%b , x0 , y0 );
	ll x1 = y0;
	ll y1 = x0 - (a/b) * y0;
	x0 = x1;
	y0 = y1;
}

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;

        y = y>>1;
        x = (x*x) % p;
    }
    return res % p;
}

double dist(double a , double b , double x, double y){ return sqrt( pow( a-x ,2) + pow( b-y ,2) ); }

double dist3(double a , double b , double c , double x , double y ,double z ){ return sqrt( pow( a-x ,2) + pow( b-y ,2) + pow( c-z ,2)); }

int xadd[9]= { 1 , -1 , 0 , 1 , -1 , 0 , 1 , -1 , 0 };
int yadd[9]= { 1 , -1 , 0 ,-1 ,  0 , 1 , 0 ,  1, -1 };

#define no  { cout<<"NO";	}
#define yes { cout<<"YES";}
#define _1 { cout<<"-1"; return 0;}

ll const N = 1e5 + 10;
ll mod = 998244353;

///////////////////////////////////////////////////////////////////

int n;
string s;

int main(){
	cin >> s;
	n = sz(s);

	ll ans =1 , c =0;

	for(int i = 1 ; i<n-1; i++){
		if(s[i]-'0' + s[i+1] - '0' == 9 && s[i-1] == s[i+1]){
			c++;
			continue;
		}
		if(c%2){
			ans *= (c+1);
		}
		c=0;
	}
	if(c%2)ans *= (c+1);

	cout << ans;
return 0;}


#include <iostream>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <map>
#include <unordered_map>
#include <stack>
#include <algorithm>
#include <stdio.h>
#include <cstring>					//  For memset
#include <sstream>

using namespace std;

#define lop(i,n) for(int i = 0 ; i < n ;i++)
#define VI	vector<int>
#define VLL	vector<ll>
#define VPI	vector<pair<int , int>>
#define VII	vector<VI>
#define all(v)	v.begin() , v.end()
#define SORT(v)	sort(all(v))
#define ll	long long
#define ull unsigned long long
#define scni(x)	scanf("%d",&x)				//  int
#define scnll(x)	scanf("%lld",&x)			//  ll
#define scnd(x)	scanf("%lf",&x)			//  double
#define prnti(x)	printf("%d\n",x)				//  int
#define prntll(x)	printf("%lld",x)				//  int
#define prntd(x)	printf("%.9f",x)				//  int
#define sz(x)  x.size()
#define SPC  <<" "<<
#define INF int(1e9)
#define pp push_back
#define P pair<int , int>
#define mp make_pair

const double EPS = 1e-9;
const double PI = 3.14159265359;

//  O( n * log(log(n)) )
vector<ll> primes;
void GeneratingPrimes(int top){
	vector<bool> p (top+1 , 1);
//	vector<ll> primes;
	p[0] = p[1] = 0;

	for(int i = 2; i <= sqrt(top) ; i++){
		if(p[i]){
			for(int j = i*i ; j <= top ; j+=i){
				p[j]=0;
			}
		}
	}
	lop(i , top+1)
		if(p[i])primes.push_back(i);
//	return primes;
}

ll gcd(ll a ,ll b){return (b==0)?a : gcd(b , a%b);}
ll lcm(ll a, ll b){return (a * b) / gcd(a , b);}

void EXTgcd(ll a , ll b , int & x0 , int & y0){
	if(b==0){ x0 =1 , y0 = 0; return; }
	EXTgcd( b , a%b , x0 , y0 );
	ll x1 = y0;
	ll y1 = x0 - (a/b) * y0;
	x0 = x1;
	y0 = y1;
}

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;

        y = y>>1;
        x = (x*x) % p;
    }
    return res % p;
}

double dist(double a , double b , double x, double y){ return sqrt( pow( a-x ,2) + pow( b-y ,2) ); }

double dist3(double a , double b , double c , double x , double y ,double z ){ return sqrt( pow( a-x ,2) + pow( b-y ,2) + pow( c-z ,2)); }

int xadd[9]= { 1 , -1 , 0 , 1 , -1 , 0 , 1 , -1 , 0 };
int yadd[9]= { 1 , -1 , 0 ,-1 ,  0 , 1 , 0 ,  1, -1 };

#define no  { cout<<"NO";	}
#define yes { cout<<"YES";}
#define _1 { cout<<"-1"; return 0;}

ll const N = 1e5 + 10;
ll mod = 998244353;

///////////////////////////////////////////////////////////////////

int n;
string s;

int main(){
	cin >> s;
	n = sz(s);

	ll ans =1 , c =0;

	for(int i = 0 ; i<n-1; i++){
		if(s[i]-'0' + s[i+1] - '0' == 9){
			c++;
			continue;
		}
		if(c%2==0){
			c /= 2;
			ans *= (c+1);
		}
		c=0;
	}
	if(c%2==0)ans *= (c/2+1);

	cout << ans;
return 0;}


#include <iostream>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <map>
#include <unordered_map>
#include <stack>
#include <algorithm>
#include <stdio.h>
#include <cstring>					//  For memset
#include <sstream>

using namespace std;

#define lop(i,n) for(int i = 0 ; i < n ;i++)
#define VI	vector<int>
#define VLL	vector<ll>
#define VPI	vector<pair<int , int>>
#define VII	vector<VI>
#define all(v)	v.begin() , v.end()
#define SORT(v)	sort(all(v))
#define ll	long long
#define ull unsigned long long
#define scni(x)	scanf("%d",&x)				//  int
#define scnll(x)	scanf("%lld",&x)			//  ll
#define scnd(x)	scanf("%lf",&x)			//  double
#define prnti(x)	printf("%d\n",x)				//  int
#define prntll(x)	printf("%lld",x)				//  int
#define prntd(x)	printf("%.9f",x)				//  int
#define sz(x)  x.size()
#define SPC  <<" "<<
#define INF int(1e9)
#define pp push_back
#define P pair<int , int>
#define mp make_pair

const double EPS = 1e-9;
const double PI = 3.14159265359;

//  O( n * log(log(n)) )
vector<ll> primes;
void GeneratingPrimes(int top){
	vector<bool> p (top+1 , 1);
//	vector<ll> primes;
	p[0] = p[1] = 0;

	for(int i = 2; i <= sqrt(top) ; i++){
		if(p[i]){
			for(int j = i*i ; j <= top ; j+=i){
				p[j]=0;
			}
		}
	}
	lop(i , top+1)
		if(p[i])primes.push_back(i);
//	return primes;
}

ll gcd(ll a ,ll b){return (b==0)?a : gcd(b , a%b);}
ll lcm(ll a, ll b){return (a * b) / gcd(a , b);}

void EXTgcd(ll a , ll b , int & x0 , int & y0){
	if(b==0){ x0 =1 , y0 = 0; return; }
	EXTgcd( b , a%b , x0 , y0 );
	ll x1 = y0;
	ll y1 = x0 - (a/b) * y0;
	x0 = x1;
	y0 = y1;
}

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;

        y = y>>1;
        x = (x*x) % p;
    }
    return res % p;
}

double dist(double a , double b , double x, double y){ return sqrt( pow( a-x ,2) + pow( b-y ,2) ); }

double dist3(double a , double b , double c , double x , double y ,double z ){ return sqrt( pow( a-x ,2) + pow( b-y ,2) + pow( c-z ,2)); }

int xadd[9]= { 1 , -1 , 0 , 1 , -1 , 0 , 1 , -1 , 0 };
int yadd[9]= { 1 , -1 , 0 ,-1 ,  0 , 1 , 0 ,  1, -1 };

#define no  { cout<<"NO";	}
#define yes { cout<<"YES";}
#define _1 { cout<<"-1"; return 0;}

ll const N = 1e5 + 10;
ll mod = 998244353;

///////////////////////////////////////////////////////////////////

int n;
string s;

int main(){
	cin >> s;
	n = sz(s);

	ll ans =1 , c =0;

	for(int i = 1 ; i<n-1; i++){
		if(s[i]-'0' + s[i+1] - '0' == 9 && s[i-1] == s[i+1]){
			c++;
			continue;
		}
		if(c%2){
			c++;
			c /= 2;
			ans *= (c+1);
		}
		c=0;
	}
	if(c%2)ans *= (c/2+2);

	cout << ans;
return 0;}


#include <iostream>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <map>
#include <unordered_map>
#include <stack>
#include <algorithm>
#include <stdio.h>
#include <cstring>					//  For memset
#include <sstream>

using namespace std;

#define lop(i,n) for(int i = 0 ; i < n ;i++)
#define VI	vector<int>
#define VLL	vector<ll>
#define VPI	vector<pair<int , int>>
#define VII	vector<VI>
#define all(v)	v.begin() , v.end()
#define SORT(v)	sort(all(v))
#define ll	long long
#define ull unsigned long long
#define scni(x)	scanf("%d",&x)				//  int
#define scnll(x)	scanf("%lld",&x)			//  ll
#define scnd(x)	scanf("%lf",&x)			//  double
#define prnti(x)	printf("%d\n",x)				//  int
#define prntll(x)	printf("%lld",x)				//  int
#define prntd(x)	printf("%.9f",x)				//  int
#define sz(x)  x.size()
#define SPC  <<" "<<
#define INF int(1e9)
#define pp push_back
#define P pair<int , int>
#define mp make_pair

const double EPS = 1e-9;
const double PI = 3.14159265359;

//  O( n * log(log(n)) )
vector<ll> primes;
void GeneratingPrimes(int top){
	vector<bool> p (top+1 , 1);
//	vector<ll> primes;
	p[0] = p[1] = 0;

	for(int i = 2; i <= sqrt(top) ; i++){
		if(p[i]){
			for(int j = i*i ; j <= top ; j+=i){
				p[j]=0;
			}
		}
	}
	lop(i , top+1)
		if(p[i])primes.push_back(i);
//	return primes;
}

ll gcd(ll a ,ll b){return (b==0)?a : gcd(b , a%b);}
ll lcm(ll a, ll b){return (a * b) / gcd(a , b);}

void EXTgcd(ll a , ll b , int & x0 , int & y0){
	if(b==0){ x0 =1 , y0 = 0; return; }
	EXTgcd( b , a%b , x0 , y0 );
	ll x1 = y0;
	ll y1 = x0 - (a/b) * y0;
	x0 = x1;
	y0 = y1;
}

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;

        y = y>>1;
        x = (x*x) % p;
    }
    return res % p;
}

double dist(double a , double b , double x, double y){ return sqrt( pow( a-x ,2) + pow( b-y ,2) ); }

double dist3(double a , double b , double c , double x , double y ,double z ){ return sqrt( pow( a-x ,2) + pow( b-y ,2) + pow( c-z ,2)); }

int xadd[9]= { 1 , -1 , 0 , 1 , -1 , 0 , 1 , -1 , 0 };
int yadd[9]= { 1 , -1 , 0 ,-1 ,  0 , 1 , 0 ,  1, -1 };

#define no  { cout<<"NO";	}
#define yes { cout<<"YES";}
#define _1 { cout<<"-1"; return 0;}

ll const N = 1e5 + 10;
ll mod = 998244353;

///////////////////////////////////////////////////////////////////

int n;
string s;

int main(){
	cin >> s;
	n = sz(s);

	ll ans =1 , c =0;

	for(int i = 1 ; i<n-1; i++){
		if(s[i]-'0' + s[i+1] - '0' == 9 && s[i-1] == s[i+1]){
			c++;
			continue;
		}
		if(c%2){
			c++;
			c /= 2;
			ans *= (c+1);
		}
		c=0;
	}
	if(c%2)ans *= (c+1);

	cout << ans;
return 0;}


#include <iostream>
#include <set>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <cmath>
#include <map>
#include <unordered_map>
#include <stack>
#include <algorithm>
#include <stdio.h>
#include <cstring>					//  For memset
#include <sstream>

using namespace std;

#define lop(i,n) for(int i = 0 ; i < n ;i++)
#define VI	vector<int>
#define VLL	vector<ll>
#define VPI	vector<pair<int , int>>
#define VII	vector<VI>
#define all(v)	v.begin() , v.end()
#define SORT(v)	sort(all(v))
#define ll	long long
#define ull unsigned long long
#define scni(x)	scanf("%d",&x)				//  int
#define scnll(x)	scanf("%lld",&x)			//  ll
#define scnd(x)	scanf("%lf",&x)			//  double
#define prnti(x)	printf("%d\n",x)				//  int
#define prntll(x)	printf("%lld",x)				//  int
#define prntd(x)	printf("%.9f",x)				//  int
#define sz(x)  x.size()
#define SPC  <<" "<<
#define INF int(1e9)
#define pp push_back
#define P pair<int , int>
#define mp make_pair

const double EPS = 1e-9;
const double PI = 3.14159265359;

//  O( n * log(log(n)) )
vector<ll> primes;
void GeneratingPrimes(int top){
	vector<bool> p (top+1 , 1);
//	vector<ll> primes;
	p[0] = p[1] = 0;

	for(int i = 2; i <= sqrt(top) ; i++){
		if(p[i]){
			for(int j = i*i ; j <= top ; j+=i){
				p[j]=0;
			}
		}
	}
	lop(i , top+1)
		if(p[i])primes.push_back(i);
//	return primes;
}

ll gcd(ll a ,ll b){return (b==0)?a : gcd(b , a%b);}
ll lcm(ll a, ll b){return (a * b) / gcd(a , b);}

void EXTgcd(ll a , ll b , int & x0 , int & y0){
	if(b==0){ x0 =1 , y0 = 0; return; }
	EXTgcd( b , a%b , x0 , y0 );
	ll x1 = y0;
	ll y1 = x0 - (a/b) * y0;
	x0 = x1;
	y0 = y1;
}

ll power(ll x, ll y, ll p)
{
    ll res = 1;

    x = x % p;

    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % p;

        y = y>>1;
        x = (x*x) % p;
    }
    return res % p;
}

double dist(double a , double b , double x, double y){ return sqrt( pow( a-x ,2) + pow( b-y ,2) ); }

double dist3(double a , double b , double c , double x , double y ,double z ){ return sqrt( pow( a-x ,2) + pow( b-y ,2) + pow( c-z ,2)); }

int xadd[9]= { 1 , -1 , 0 , 1 , -1 , 0 , 1 , -1 , 0 };
int yadd[9]= { 1 , -1 , 0 ,-1 ,  0 , 1 , 0 ,  1, -1 };

#define no  { cout<<"NO";	}
#define yes { cout<<"YES";}
#define _1 { cout<<"-1"; return 0;}

ll const N = 1e5 + 10;
ll mod = 998244353;

///////////////////////////////////////////////////////////////////

int n;
string s;

int main(){
	cin >> s;
	n = sz(s);

	ll ans =1 , c =0;

	for(int i = 1 ; i<n-1; i++){
		if(s[i]-'0' + s[i+1] - '0' == 9 && s[i-1] == s[i+1]){
			c++;
			continue;
		}
		if(c%2){
			c /= 2;
			c += 2;
			ans *= (c+1);
		}
		c=0;
	}
	if(c%2)ans *= (c+1);

	cout << ans;
return 0;}


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
typedef long long LL;

char a[100005];
bool p[100005];

int main()
{
	int n,m,i,j;
	scanf("%s",a);
	for (int i=0;a[i]!='\0';i++)
	{
		if (a[i]-'0'+a[i+1]-'0'==9) p[i]=true;
	}
	int num=0;
	for (int i=0;a[i]!='\0';i++)
	if (p[i])
	{
		num++;i++;
	}
	int ans=1;
	int sum=0;
	for (int i=0;a[i]!='\0';i++)
	if (p[i]) sum++;
	else if (sum>=1)
	{
		if (sum%2==0) ans*=2;
		sum=0;
	}
	printf("%d\n",ans);
}
//110101010
//11101010
//45454


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
typedef long long LL;

char a[100005];
bool p[100005];

int main()
{
	int n,m,i,j;
	scanf("%s",a);
	for (int i=0;a[i]!='\0';i++)
	{
		if (a[i]-'0'+a[i+1]-'0'==9) p[i]=true;
	}
	int num=0;
	for (int i=0;a[i]!='\0';i++)
	if (p[i])
	{
		num++;i++;
	}
	LL ans=1;
	LL sum=0;
	for (int i=0;a[i]!='\0';i++)
	if (p[i]) sum++;
	else if (sum>=1)
	{
		if (sum%2==0) ans*=2;
		sum=0;
	}
	printf("%I64d\n",ans);
}
//110101010
//11101010
//45454


#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
typedef long long LL;

char a[100005];
bool p[100005];

int main()
{
	int n,m,i,j;
	scanf("%s",a);
	for (int i=0;a[i]!='\0';i++)
	{
		if (a[i]-'0'+a[i+1]-'0'==9) p[i]=true;
	}
	int num=0;
	for (int i=0;a[i]!='\0';i++)
	if (p[i])
	{
		num++;i++;
	}
	LL ans=1;
	LL sum=0;
	for (int i=0;a[i]!='\0';i++)
	if (p[i]) sum++;
	else if (sum>=1)
	{
		if (sum%2==0) ans*=sum/2+1;
		sum=0;
	}
	printf("%I64d\n",ans);
}
//110101010
//11101010
//45454


#include<bits/stdc++.h>
using namespace std;
#define Maxn 10000100
typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> ii;
vector <vi> adjlist;
set<ii> sol;
int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	ll ans;
	string n;
	while (cin >> n)
	{
		ans = 1;
		int s = 1;
		for (int i = 0; i < n.length()-1; i++)
		{
			if ((int)(n[i] + n[i + 1] )- '0' - '0' == 9)
				s++;
			else
			{
				if (s % 2)
					ans *= (s + 1) / 2;
				s = 1;
			}
		}
		if (s % 2) ans *= (s + 1) / 2;
		cout << ans << endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
#define Maxn 10000100
typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> ii;
vector <vi> adjlist;
set<ii> sol;
int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	ll ans;
	string n;
	while (cin >> n)
	{
		ans = 1;

		for (int i = 0; i < n.length(); i++)
		{
			if (i + 3 < n.length())
			{
				if ((int)(n[i] + n[i + 1] - '0' - '0' )== 9 && (int)(n[i + 1] + n[i + 2] - '0' - '0' )== 9 && (int)(n[i + 2] + n[i + 3] - '0' - '0') != 9)
				{
					ans *= 2;
					i++;
				}
			}
			else
			{
				if (i + 2 < n.length())
				{
					if ((int)(n[i] + n[i + 1] - '0' - '0') == 9 && (int)(n[i + 1] + n[i + 2] - '0' - '0' )== 9)
					{
						ans *= 2;
					}
				}
			}
		}
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    string r;
    cin>>r;
    long long tot=1;
    for(int i=0;i<r.length()-1;i++)
    {
        int t=r[i]-'0';
        long long cnt=1;
        int t2=r[i+1]-'0';
        if(t+t2==9){
        while(t+t2==9)
        {
            i++;
            t=r[i]-'0';
            cnt++;
            t2=r[i+1]-'0';
            //cout<<i<<endl;
        }
        if(cnt%2!=0)
        {
            //cout<<i<<" wow ";
            tot=tot*(cnt+1)/2;
        }}
    }
    cout<<tot;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    string r;
    cin>>r;
    long long tot=1;
    for(int i=0;i<r.length()-1;i++)
    {
        int t=r[i]-'0';
        long long cnt=1;
        int t2=r[i+1]-'0';
        if(t+t2==9){
        while(t+t2==9)
        {
            i++;
            t=r[i]-'0';
            cnt++;
            cnt=cnt%2;
            t2=r[i+1]-'0';
            //cout<<i<<endl;
        }
        if(cnt%2!=0)
        {
            //cout<<i<<" wow ";
            tot=tot*2;
        }}
    }
    cout<<tot;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main()
{
    string r;
    cin>>r;
    long long tot=1;
    for(int i=0;i<r.length()-1;i++)
    {
        int t=r[i]-'0';
        long long cnt=1;
        int t2=r[i+1]-'0';
        while(t+t2==9)
        {
            i++;
            t=r[i]-'0';
            cnt++;
            cnt=cnt%2;
            t2=r[i+1]-'0';
        }
        if(cnt%2!=0)
        {
            tot=tot*2;
        }
    }
    cout<<tot;

    return 0;
}


#include <bits/stdc++.h>
#define f(i,n) for(int i=0;i<n;i++)
#define R(i,A,B) for(int i=A;i<B;i++)
#define fi first
#define se second
#define M 1000000007
#define PB push_back
#define ll long long
#define sd(x) scanf("%d",&x)
#define pii pair<int,int>
#define mod 1000000007
using namespace std;
ll int b,w,c,n,k,m,l,r;
//int dp[100003]={};
map<ll int,ll int>in;
map<ll int,ll int>out;
map<int,int>mark;
int co(char ch){
    return ch - '0';
}
ll int maxx(ll int g,ll int h){
    if(g>h)return g;
    return h;
}
ll int minn(ll int g,ll int h){
    if(g>h)return h;
    return g;
}
int used[102]={};
void solve(){
    string s;
    cin>>s;
    n = s.length();
    ll int ans = 1;
    ll int cnt = 0;
    int i = 0;
    while(i<n-1){
        if(co(s[i])+co(s[i+1]) == 9){
            cnt++;
            i++;
        }
        else{
            if(cnt!=0){
                if(cnt&1){
                    cnt = 0;
                }
                else{
                    ans*=(cnt/2)+1;
                    cnt = 0;
                }
            }
            i++;
        }
        if(i == n-1){
            if(cnt!=0){
                if(cnt&1){
                    cnt = 0;
                }
                else{
                    ans*=(cnt/2)+1;
                    cnt = 0;
                }
            }
        }
    }
    cout<<ans;
}
int main(){
    int t = 1;
   // cin>>t;
    while(t--)solve();
}


#include <bits/stdc++.h>
#define f(i,n) for(int i=0;i<n;i++)
#define R(i,A,B) for(int i=A;i<B;i++)
#define fi first
#define se second
#define M 1000000007
#define PB push_back
#define ll long long
#define sd(x) scanf("%d",&x)
#define pii pair<int,int>
#define mod 1000000007
using namespace std;
ll int b,w,c,n,k,m,l,r;
//int dp[100003]={};
map<ll int,ll int>in;
map<ll int,ll int>out;
map<int,int>mark;
int co(char ch){
    return ch - '0';
}
ll int maxx(ll int g,ll int h){
    if(g>h)return g;
    return h;
}
ll int minn(ll int g,ll int h){
    if(g>h)return h;
    return g;
}
int used[102]={};
void solve(){
    string s;
    cin>>s;
    n = s.length();
    ll int ans = 1;
    ll int cnt = 0;
    int i = 0;
    while(i<n-1){
        if(co(s[i]) + co(s[i+1]) ==  9){
            if( i<n-3 && (co(s[i+2]) + co(s[i+3]) == 9)){
                cnt++;
                i+=4;
                ans*=cnt;
                cnt = 0;
            }
            else{
                cnt++;//cout<<cnt;
                i++;
            }
        }
        else{
           if(cnt!=0) ans*=cnt;
            cnt = 0;
            i++;
        }
        if(i == n-1 && cnt!=0){
            ans *= cnt;
        }
    }
    cout<<ans;
}
int main(){
    int t = 1;
    //cin>>t;
    while(t--)solve();
}


#include <bits/stdc++.h>
#define f(i,n) for(int i=0;i<n;i++)
#define R(i,A,B) for(int i=A;i<B;i++)
#define fi first
#define se second
#define M 1000000007
#define PB push_back
#define ll long long
#define sd(x) scanf("%d",&x)
#define pii pair<int,int>
#define mod 1000000007
using namespace std;
ll int b,w,c,n,k,m,l,r;
//int dp[100003]={};
map<ll int,ll int>in;
map<ll int,ll int>out;
map<int,int>mark;
int co(char ch){
    return ch - '0';
}
ll int maxx(ll int g,ll int h){
    if(g>h)return g;
    return h;
}
ll int minn(ll int g,ll int h){
    if(g>h)return h;
    return g;
}
int used[102]={};
void solve(){
    string s;
    cin>>s;
    n = s.length();
    ll int ans = 1;
    ll int cnt = 0;
    int i = 0;
    while(i<n-1){
        if(co(s[i]) + co(s[i+1]) ==  9){
            if(i<n-3 && (co(s[i+2])+co(s[i+3]) == 9)){
                i+=2;
                cnt++;
                ans*=cnt;
                cnt = 0;
            }
            else{
                cnt++;
                i++;
            }
        }
        else{
            if(cnt!=0)ans*=cnt;
            cnt = 0;
            i++;
        }
        if(i == n-1){
            if(cnt!=0)ans*=cnt;
        }
    }
    cout<<ans;
}
int main(){
    int t = 1;
    //cin>>t;
    while(t--)solve();
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<sstream>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<queue>
#include<functional>
#include<numeric>
#include<algorithm>

#define ll long long int
#define pb push_back
#define mp make_pair
#define pp(type) pair<type,type>
#define sz(n) strlen(n)
#define bs(a,n) binary_search(a.begin(), a.end(), n)
#define lb(a,n) lower_bound(a.begin(), a.end(), n) - a.begin()
#define ub(a,n) upper_bound(a.begin(), a.end(), n) - a.begin()
#define repf(i,val,n) for(int i=val;i<n;i++)
#define repfe(i,val,n) for(int i=val;i<=n;i++)
#define repb(i,n,val) for(int i=n;i>val;i--)
#define repbe(i,n,val) for(int i=n;i>=val;i--)
#define dbg(x) cerr << #x << " = " << x << endl;
#define ms(a,v) memset(a, v, sizeof a)

#define si(n) scanf("%d",&n);
#define sl(n) scanf("%ld",&n);
#define sll(n) scanf("%I64d",&n);
#define sc(n) scanf("%c",&n);
#define ss(n) scanf("%s",&n);
#define sf(n) scanf("%f",&n);
#define sd(n) scanf("%lf",&n);
#define sld(n) scanf("%Lf",&n);
#define PI 3.14159265358979323846264338327950
#define mod 1000000007

using namespace std;

string tostr(long long x){ stringstream ss; ss << x; return ss.str(); }
long long toint(const string &s){ stringstream ss; ss << s; long long x; ss >> x; return x; }

int main()
{
	string s;
	cin>>s;
	int n = s.size();
	if(n==1)
		cout<<"1";
	else
	{
		vector<bool> t(n,false);
		repf(i,1,n)
			if((s[i]-'0')+(s[i-1]-'0') == 9)
				t[i-1] = true;

		long long int ans = 1;
		long long int cur = t[0]?1:0;
		repf(i,1,n)
		{
			if(t[i])
				++cur;
			else
			{
				if(cur>0 && cur%2==0)
					ans *= cur/2+1;
				cur = 0;
			}
		}
		cout<<ans;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<sstream>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<queue>
#include<functional>
#include<numeric>
#include<algorithm>

#define ll long long int
#define pb push_back
#define mp make_pair
#define pp(type) pair<type,type>
#define sz(n) strlen(n)
#define bs(a,n) binary_search(a.begin(), a.end(), n)
#define lb(a,n) lower_bound(a.begin(), a.end(), n) - a.begin()
#define ub(a,n) upper_bound(a.begin(), a.end(), n) - a.begin()
#define repf(i,val,n) for(int i=val;i<n;i++)
#define repfe(i,val,n) for(int i=val;i<=n;i++)
#define repb(i,n,val) for(int i=n;i>val;i--)
#define repbe(i,n,val) for(int i=n;i>=val;i--)
#define dbg(x) cerr << #x << " = " << x << endl;
#define ms(a,v) memset(a, v, sizeof a)

#define si(n) scanf("%d",&n);
#define sl(n) scanf("%ld",&n);
#define sll(n) scanf("%I64d",&n);
#define sc(n) scanf("%c",&n);
#define ss(n) scanf("%s",&n);
#define sf(n) scanf("%f",&n);
#define sd(n) scanf("%lf",&n);
#define sld(n) scanf("%Lf",&n);
#define PI 3.14159265358979323846264338327950
#define mod 1000000007

using namespace std;

string tostr(long long x){ stringstream ss; ss << x; return ss.str(); }
long long toint(const string &s){ stringstream ss; ss << s; long long x; ss >> x; return x; }

int main()
{
	string s;
	cin>>s;
	int n = s.size();
	if(n==1)
		cout<<"1";
	else
	{
		long long int ans = 1,len=0;
		repf(i,1,n)
		{
			if((s[i]-'0')+(s[i-1]-'0') == 9)
				len++;
			else
			{
				if(len!=0)
					ans*=len;
				len=0;
			}
		}
		cout<<ans;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<sstream>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<queue>
#include<functional>
#include<numeric>
#include<algorithm>

#define ll long long int
#define pb push_back
#define mp make_pair
#define pp(type) pair<type,type>
#define sz(n) strlen(n)
#define bs(a,n) binary_search(a.begin(), a.end(), n)
#define lb(a,n) lower_bound(a.begin(), a.end(), n) - a.begin()
#define ub(a,n) upper_bound(a.begin(), a.end(), n) - a.begin()
#define repf(i,val,n) for(int i=val;i<n;i++)
#define repfe(i,val,n) for(int i=val;i<=n;i++)
#define repb(i,n,val) for(int i=n;i>val;i--)
#define repbe(i,n,val) for(int i=n;i>=val;i--)
#define dbg(x) cerr << #x << " = " << x << endl;
#define ms(a,v) memset(a, v, sizeof a)

#define si(n) scanf("%d",&n);
#define sl(n) scanf("%ld",&n);
#define sll(n) scanf("%I64d",&n);
#define sc(n) scanf("%c",&n);
#define ss(n) scanf("%s",&n);
#define sf(n) scanf("%f",&n);
#define sd(n) scanf("%lf",&n);
#define sld(n) scanf("%Lf",&n);
#define PI 3.14159265358979323846264338327950
#define mod 1000000007

using namespace std;

string tostr(long long x){ stringstream ss; ss << x; return ss.str(); }
long long toint(const string &s){ stringstream ss; ss << s; long long x; ss >> x; return x; }

int main()
{
	string s;
	cin>>s;
	int n = s.size();
	if(n==1)
		cout<<"1";
	else
	{
		long long int ans = 1,len=0;
		repf(i,1,n)
		{
			if((s[i]-'0')+(s[i-1]-'0') == 9)
				len++;
			else
			{
				if(len!=0)
					ans*=len;
				len=0;
			}
		}
		if(len==n-1)
		{
			if(n%2==0)
				ans=1;
			else
				ans = (len/2)+1;
		}
		else if(len!=0)
			ans*=len;
		cout<<ans;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<sstream>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<queue>
#include<functional>
#include<numeric>
#include<algorithm>

#define ll long long int
#define pb push_back
#define mp make_pair
#define pp(type) pair<type,type>
#define sz(n) strlen(n)
#define bs(a,n) binary_search(a.begin(), a.end(), n)
#define lb(a,n) lower_bound(a.begin(), a.end(), n) - a.begin()
#define ub(a,n) upper_bound(a.begin(), a.end(), n) - a.begin()
#define repf(i,val,n) for(int i=val;i<n;i++)
#define repfe(i,val,n) for(int i=val;i<=n;i++)
#define repb(i,n,val) for(int i=n;i>val;i--)
#define repbe(i,n,val) for(int i=n;i>=val;i--)
#define dbg(x) cerr << #x << " = " << x << endl;
#define ms(a,v) memset(a, v, sizeof a)

#define si(n) scanf("%d",&n);
#define sl(n) scanf("%ld",&n);
#define sll(n) scanf("%I64d",&n);
#define sc(n) scanf("%c",&n);
#define ss(n) scanf("%s",&n);
#define sf(n) scanf("%f",&n);
#define sd(n) scanf("%lf",&n);
#define sld(n) scanf("%Lf",&n);
#define PI 3.14159265358979323846264338327950
#define mod 1000000007

using namespace std;

string tostr(long long x){ stringstream ss; ss << x; return ss.str(); }
long long toint(const string &s){ stringstream ss; ss << s; long long x; ss >> x; return x; }

int main()
{
	string s;
	cin>>s;
	int n = s.size();
	if(n==1)
		cout<<"1";
	else
	{
		long long int ans = 1,len=0;
		repf(i,1,n)
		{
			if((s[i]-'0')+(s[i-1]-'0') == 9)
				len++;
			else
			{
				if(len!=0)
					ans*=len;
				len=0;
			}
		}
		if(len!=0)
			ans*=len;
		cout<<ans;
	}
	return 0;
}


#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;

int a[100010];
char d[100010];
int main () {
	scanf ("%s", d);
	int l, n;
	ull cnt, tem;
	l = strlen(d);
	cnt = 1;
	tem = 0;
	memset (a, 0, sizeof(a));
	d[0] -= '0';
	for (int i = 0; i < l - 1; i++) {
		d[i + 1] -= '0';
		if (d[i] + d[i + 1] == 9) {
			a[i] += 1;
			a[i + 1] += 1;
		}
	}
	for (int i = 0; i < l; i++) {
		if (a[i] == 2) tem++;
		else {
			if (tem && tem % 2) {
				cnt *= (tem + 3) / 2;
			}
			tem = 0;
		}
	}
	cout << cnt << endl;
	return 0;
}

/*

*/


#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

int a[100010];
char d[100010];
int main () {
	scanf ("%s", d);
	int l, n , tem, cnt;
	l = strlen(d);
	cnt = 1;
	tem = 0;
	memset (a, 0, sizeof(a));
	d[0] -= '0';
	for (int i = 0; i < l - 1; i++) {
		d[i + 1] -= '0';
		if (d[i] + d[i + 1] == 9) {
			a[i] += 1;
			a[i + 1] += 1;
		}
	}
	for (int i = 0; i < l; i++) {
		if (a[i] == 2) tem++;
		else {
			if (tem) {
				cnt *= (tem + 3) / 2;
			}
			tem = 0;
		}
	}
	cout << cnt << endl;
	return 0;
}

/*

*/


#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

int a[100010];
char d[100010];
int main () {
	scanf ("%s", d);
	int l, n;
	ll cnt, tem;
	l = strlen(d);
	cnt = 1;
	tem = 0;
	memset (a, 0, sizeof(a));
	d[0] -= '0';
	for (int i = 0; i < l - 1; i++) {
		d[i + 1] -= '0';
		if (d[i] + d[i + 1] == 9) {
			a[i] += 1;
			a[i + 1] += 1;
		}
	}
	for (int i = 0; i < l; i++) {
		if (a[i] == 2) tem++;
		else {
			if (tem) {
				cnt *= 1ll * (tem + 3) / 2;
			}
			tem = 0;
		}
	}
	cout << cnt << endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
	char a[100100];
	scanf("%s", a);
	long long ans = 1;
	for (int i = 1; a[i]; ++i) {
		if (a[i] + a[i - 1] - '0' == '9') {
			int cnt = 2;
			++i;
			while (a[i] && a[i] == a[i - 2]) {
				++i;
				++cnt;
			}
			if (cnt & 1) ans *= ((1 + cnt) >> 1);
			--i;
		}
	}
	cout << ans << '\n';
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
char s[1005][1005];
int dp[1005][1005];
int n,m,INF=1000*1000*1000;
int mx=0;
int dx[]={0,0,-1,1},dy[]={-1,1,0,0};

int floodfill(int i, int j, int cnt)
{

    if(cnt>500000)
    {
        cout << "Poor Inna!" << endl;
        exit(0);
    }

    if(i>n||j>m||j<1||i<1) return 0;

    if(dp[i][j]) return dp[i][j];

    char se = 'I';
    if(s[i][j]=='I') se = 'M';
    if(s[i][j]=='M') se = 'A';
    if(s[i][j]=='A') se = 'D';
    int ret = cnt;

    for(int k = 0; k < 4; ++k) if(s[i+dx[k]][j+dy[k]] == se) ret = max(cnt + floodfill(i+dx[k],j+dy[k],1),ret);

    return dp[i][j] = ret;
}

int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; ++i)
    {
        scanf("%s", &s[i][1]);
    }
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= m; ++j)
        {
            if(s[i][j]=='D')
            {
                mx = max(mx,floodfill(i,j,1)/4);
            }
        }
    }

    if(mx==0) cout << "Poor Dima!" << endl;
    else cout << mx << endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>

#define oo 1e9
#define pi 3.1415926536
#define all(x) x.begin(),x.end()
#define sorta(x) sort(all(x))
#define sortam(x,comp) sort(all(x),comp)
#define sortd(x) sort(x.rbegin(),x.rend())
#define pb push_back
#define mp make_pair
#define sf(x) scanf("%d", &x);
#define sfll(x) scanf("%I64d", &x);
#define pr(x) printf("%d ", x);

typedef long long ll;
using namespace std;

vector<string>in;
vector<vector<bool> >vis;
string dima = "DIMA";
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int mem[1010][1010];

bool valid(int i, int j, int ind) {
	return i >= 0 && i < in.size() && j >= 0 && j < in[0].size() && dima[ind % 4] == in[i][j];
}

int go(int i, int j, int ind) {
	if(vis[i][j] && in[i][j] == 'D') return oo;
	if(mem[i][j] != -1) return mem[i][j];

	vis[i][j] = 1;
	int ret = 0;
	for(int k = 0; k < 4; k++) {
		if(valid(i + dx[k], j + dy[k], ind)) ret = max(ret, go(i + dx[k], j + dy[k], (ind + 1) % 4));
	}

	return mem[i][j] = ret + !ind;
}

int main() {
	int n, m;
	while(cin >> n>>m) {
		memset(mem, -1, sizeof mem);
		in.resize(n);
		vis.assign(n, vector<bool>(m, 0));
		for(int i = 0; i < n; cin >> in[i++]);

		int res = 0;
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < m; j++) {
				if(!vis[i][j] && in[i][j] == 'D') res = max(res, go(i, j, 1));
			}
		}

		if(!res) cout << "Poor Dima!\n";
		else if(res >= oo) cout << "Poor Inna!\n";
		else cout << res << endl;
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>

#define oo 1e9
#define pi 3.1415926536
#define all(x) x.begin(),x.end()
#define sorta(x) sort(all(x))
#define sortam(x,comp) sort(all(x),comp)
#define sortd(x) sort(x.rbegin(),x.rend())
#define pb push_back
#define mp make_pair
#define sf(x) scanf("%d", &x);
#define sfll(x) scanf("%I64d", &x);
#define pr(x) printf("%d ", x);

typedef long long ll;
using namespace std;

char in[1010][1010];
vector<vector<int> >vis;
string dima = "DIMA";
int n, m;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int mem[1010][1010];

bool valid(int i, int j, int ind) {
    return i >= 0 && i < n && j >= 0 && j < m && dima[ind % 4] == in[i][j];
}

bool loop(int i, int j) {
    if(vis[i][j]) return 1;

    int ind = (dima.find(in[i][j]) + 1) % 4;
    vis[i][j] = 1;
    bool ret = 0;
    for(int k = 0; k < 4; k++) {
        if(valid(i + dx[k], j + dy[k], ind) && vis[i + dx[k]][j + dy[k]] != 2) ret |= loop(i + dx[k], j + dy[k]);
    }
    vis[i][j] = 2;
    return ret;
}

int go(int i, int j) {
    if(mem[i][j] != -1) return mem[i][j];

    int ind = (dima.find(in[i][j]) + 1) % 4;
    int ret = 0;
    for(int k = 0; k < 4; k++) {
        if(valid(i + dx[k], j + dy[k], ind)) ret = max(ret, go(i + dx[k], j + dy[k]));
    }

    return mem[i][j] = ret + !ind;
}

int main() {
    sf(n); sf(m);
    memset(mem, -1, sizeof mem);
    vis.assign(n, vector<int>(m, 0));
    for(int i = 0; i < n; i++) scanf("%s", in[i]);

    bool lp = 0;
    for(int i = 0; i < n && !lp; i++) {
        for(int j = 0; j < m && !lp; j++) {
            if(in[i][j] == 'D' && vis[i][j]!=2 && loop(i, j)) lp = 1;
        }
    }

    if(lp) {
        cout << "Poor Inna!\n";
        return 0;
    }

    int res = 0;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if(in[i][j] == 'D') res = max(res, go(i, j));
        }
    }

    if(!res) cout << "Poor Dima!\n";
    else cout << res << endl;

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>

#define oo 1e9
#define pi 3.1415926536
#define all(x) x.begin(),x.end()
#define sorta(x) sort(all(x))
#define sortam(x,comp) sort(all(x),comp)
#define sortd(x) sort(x.rbegin(),x.rend())
#define pb push_back
#define mp make_pair
#define sf(x) scanf("%d", &x);
#define sfll(x) scanf("%I64d", &x);
#define pr(x) printf("%d ", x);

typedef long long ll;
using namespace std;

vector<string>in;
vector<vector<bool> >vis;
string dima = "DIMA";
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int mem[1010][1010];

bool valid(int i, int j, int ind) {
    return i >= 0 && i < in.size() && j >= 0 && j < in[0].size() && dima[ind % 4] == in[i][j];
}

bool loop(int i, int j, int ind) {
    if(vis[i][j]) return 1;

    vis[i][j] = 1;
    bool ret = 0;
    for(int k = 0; k < 4; k++) {
        if(valid(i + dx[k], j + dy[k], ind)) ret |= loop(i + dx[k], j + dy[k], (ind + 1) % 4);
    }
    vis[i][j] = 0;
    return ret;
}

int go(int i, int j, int ind) {
    if(mem[i][j] != -1) return mem[i][j];

    int ret = 0;
    for(int k = 0; k < 4; k++) {
        if(valid(i + dx[k], j + dy[k], ind)) ret = max(ret, go(i + dx[k], j + dy[k], (ind + 1) % 4));
    }

    return mem[i][j] = ret + !ind;
}

int main() {
    int n, m;
    while(cin >> n>>m) {
        memset(mem, -1, sizeof mem);
        in.resize(n);
        vis.assign(n, vector<bool>(m, 0));
        for(int i = 0; i < n; cin >> in[i++]);

        bool lp = 0;
        for(int i = 0; i < n && !lp; i++) {
            for(int j = 0; j < m && !lp; j++) {
                if(in[i][j] == 'D' && loop(i,j,1)) lp=1;
            }
        }

        if(lp) {
            cout << "Poor Inna!\n";
            continue;
        }

        int res = 0;
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                if(in[i][j] == 'D') res = max(res, go(i, j, 1));
            }
        }

        if(!res) cout << "Poor Dima!\n";
        else cout << res << endl;
    }
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>

#define oo 1e9
#define pi 3.1415926536
#define all(x) x.begin(),x.end()
#define sorta(x) sort(all(x))
#define sortam(x,comp) sort(all(x),comp)
#define sortd(x) sort(x.rbegin(),x.rend())
#define pb push_back
#define mp make_pair
#define sf(x) scanf("%d", &x);
#define sfll(x) scanf("%I64d", &x);
#define pr(x) printf("%d ", x);

typedef long long ll;
using namespace std;

vector<string>in;
vector<vector<bool> >vis;
string dima = "DIMA";
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int mem[1010][1010];
bool lp;

bool valid(int i, int j, int ind) {
    return i >= 0 && i < in.size() && j >= 0 && j < in[0].size() && dima[ind % 4] == in[i][j];
}

int go(int i, int j, int ind) {
    if(vis[i][j] && in[i][j] == 'D') { lp = 1; return 0; }
    if(mem[i][j] != -1) return mem[i][j];

    vis[i][j] = 1;
    int ret = 0;
    for(int k = 0; k < 4; k++) {
        if(valid(i + dx[k], j + dy[k], ind)) ret = max(ret, go(i + dx[k], j + dy[k], (ind + 1) % 4));
    }

    return mem[i][j] = ret + !ind;
}

int main() {
    int n, m;
    while(cin >> n>>m) {
        lp = 0;
        memset(mem, -1, sizeof mem);
        in.resize(n);
        vis.assign(n, vector<bool>(m, 0));
        for(int i = 0; i < n; cin >> in[i++]);

        int res = 0;
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                if(!vis[i][j] && in[i][j] == 'D') res = max(res, go(i, j, 1));
            }
        }

        if(lp) cout << "Poor Inna!\n";
        else if(!res) cout << "Poor Dima!\n";
        else cout << res << endl;
    }
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>

#define oo 1e9
#define pi 3.1415926536
#define all(x) x.begin(),x.end()
#define sorta(x) sort(all(x))
#define sortam(x,comp) sort(all(x),comp)
#define sortd(x) sort(x.rbegin(),x.rend())
#define pb push_back
#define mp make_pair
#define sf(x) scanf("%d", &x);
#define sfll(x) scanf("%I64d", &x);
#define pr(x) printf("%d ", x);

typedef long long ll;
using namespace std;

char in[1010][1010];
vector<vector<bool> >vis;
string dima = "DIMA";
    int n, m;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int mem[1010][1010];

bool valid(int i, int j, int ind) {
    return i >= 0 && i < n && j >= 0 && j < m && dima[ind % 4] == in[i][j];
}

bool loop(int i, int j) {
    if(vis[i][j]) return 1;

    int ind = (dima.find(in[i][j]) + 1) % 4;
    vis[i][j] = 1;
    bool ret = 0;
    for(int k = 0; k < 4; k++) {
        if(valid(i + dx[k], j + dy[k], ind)) ret |= loop(i + dx[k], j + dy[k]);
    }
    vis[i][j] = 0;
    return ret;
}

int go(int i, int j) {
    if(mem[i][j] != -1) return mem[i][j];

    int ind = (dima.find(in[i][j]) + 1) % 4;
    int ret = 0;
    for(int k = 0; k < 4; k++) {
        if(valid(i + dx[k], j + dy[k], ind)) ret = max(ret, go(i + dx[k], j + dy[k]));
    }

    return mem[i][j] = ret + !ind;
}

int main() {
    sf(n); sf(m);
    memset(mem, -1, sizeof mem);
    vis.assign(n, vector<bool>(m, 0));
    for(int i = 0; i < n; i++) scanf("%s", in[i]);

    bool lp = 0;
    for(int i = 0; i < n && !lp; i++) {
        for(int j = 0; j < m && !lp; j++) {
            if(in[i][j] == 'D' && loop(i, j)) lp = 1;
        }
    }

    if(lp) {
        cout << "Poor Inna!\n";
        return 0;
    }

    int res = 0;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if(in[i][j] == 'D') res = max(res, go(i, j));
        }
    }

    if(!res) cout << "Poor Dima!\n";
    else cout << res << endl;

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <vector>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>

#define oo 1e9
#define pi 3.1415926536
#define all(x) x.begin(),x.end()
#define sorta(x) sort(all(x))
#define sortam(x,comp) sort(all(x),comp)
#define sortd(x) sort(x.rbegin(),x.rend())
#define pb push_back
#define mp make_pair
#define sf(x) scanf("%d", &x);
#define sfll(x) scanf("%I64d", &x);
#define pr(x) printf("%d ", x);

typedef long long ll;
using namespace std;

char in[1010][1010];
vector<vector<bool> >vis;
string dima = "DIMA";
    int n, m;
int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
int mem[1010][1010];

bool valid(int i, int j, int ind) {
    return i >= 0 && i < n && j >= 0 && j < m && dima[ind % 4] == in[i][j];
}

bool loop(int i, int j) {
    if(vis[i][j]) return 1;

    int ind = (dima.find(in[i][j]) + 1) % 4;
    vis[i][j] = 1;
    bool ret = 0;
    for(int k = 0; k < 4; k++) {
        if(valid(i + dx[k], j + dy[k], ind)) ret |= loop(i + dx[k], j + dy[k]);
    }
    //vis[i][j] = 0;
    return ret;
}

int go(int i, int j) {
    if(mem[i][j] != -1) return mem[i][j];

    int ind = (dima.find(in[i][j]) + 1) % 4;
    int ret = 0;
    for(int k = 0; k < 4; k++) {
        if(valid(i + dx[k], j + dy[k], ind)) ret = max(ret, go(i + dx[k], j + dy[k]));
    }

    return mem[i][j] = ret + !ind;
}

int main() {
    sf(n); sf(m);
    memset(mem, -1, sizeof mem);
    vis.assign(n, vector<bool>(m, 0));
    for(int i = 0; i < n; i++) scanf("%s", in[i]);

    bool lp = 0;
    for(int i = 0; i < n && !lp; i++) {
        for(int j = 0; j < m && !lp; j++) {
            if(in[i][j] == 'D' && loop(i, j)) lp = 1;
        }
    }

    if(lp) {
        cout << "Poor Inna!\n";
        return 0;
    }

    int res = 0;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            if(in[i][j] == 'D') res = max(res, go(i, j));
        }
    }

    if(!res) cout << "Poor Dima!\n";
    else cout << res << endl;

    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;

#define MAX 1003
#define INF 100000008

string arr[MAX],dim="DIMA";
int n,m,vis[MAX][MAX];
int X[]={0,0,1,-1};
int Y[]={1,-1,0,0};

int go(int x,int y,int next,int cnt){
    //cout<<x<<' '<<y<<' '<<next<<' '<<cnt<<endl;
    if(vis[x][y]){
        //cout<<x<<' '<<y<<"=="<<endl;
        if(vis[x][y]==cnt && arr[x][y]=='D')
            return INF;
        return 0;
    }
    vis[x][y]=cnt;

    int ans=0,t;

    if(arr[x][y]=='A')ans=1;

    for(int i=0;i<4;i++){
        int a=x+X[i],b=y+Y[i];
        char p=dim[next];
        //cout<<a<<' '<<b<<' '<<p<<endl;
        if(a<0 || a>=n || b<0 || b>=m || arr[a][b] != p)continue;

        t=go(a,b,(next+1)%4,cnt);
        if(t==INF)return t;
        ans+=t;
    }

    return ans;
}

int main(){
    //freopen("input.txt","r",stdin);
    int i,j,ans=0,cnt=1;
    cin>>n>>m;
    for(i=0;i<n;i++)
        cin>>arr[i];
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            if(!vis[i][j] && arr[i][j]=='D'){

                ans=max(ans,go(i,j,1,cnt));

                //if(i==0 && j==0)
                    //return 0;//cout<<t<<endl;
                if(ans==INF){
                    //cout<<i<<' '<<j<<endl;
                    return cout<<"Poor Dima!"<<endl,0;
                }
                cnt++;
            }

        }
    }
    if(ans==0)
        cout<<"Poor Dima!"<<endl;
    else cout<<ans<<endl;

    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;

#define MAX 1003
#define INF 100000008

string arr[MAX],dim="DIMA";
int n,m,vis[MAX][MAX],ANS[MAX][MAX];
int X[]={0,0,1,-1};
int Y[]={1,-1,0,0};

int go(int x,int y,int next){
    if(vis[x][y]){
        if(vis[x][y]==2)
            return INF;
        return ANS[x][y];
    }
    ANS[x][y]=1;
    vis[x][y]=1;

    int t=0;

    for(int i=0;i<4;i++){
        int a=x+X[i],b=y+Y[i];
        char p=dim[next];
        if(a<0 || a>=n || b<0 || b>=m || arr[a][b] != p)continue;

        t=max(t,go(a,b,(next+1)%4));
        if(t==INF)return t;
    }

    ANS[x][y]+=t;

    vis[x][y]=2;

    return ANS[x][y];
}

int main(){
    //freopen("input.txt","r",stdin);
    int i,j,ans=0;
    cin>>n>>m;
    for(i=0;i<n;i++)
        cin>>arr[i];
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            if(!vis[i][j] && arr[i][j]=='D'){

                ans=max(ans,go(i,j,1));
                if(ans==INF){
                    return cout<<"Poor Inna!"<<endl,0;
                }
            }

        }
    }
    if(ans<4)
        cout<<"Poor Dima!"<<endl;
    else cout<<ans/4<<endl;

    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;

#define MAX 1003
#define INF 100000008

string arr[MAX],dim="DIMA";
int n,m,vis[MAX][MAX];
int X[]={0,0,1,-1};
int Y[]={1,-1,0,0};

int go(int x,int y,int next,int cnt){
    //cout<<x<<' '<<y<<' '<<next<<' '<<cnt<<endl;
    if(vis[x][y]){
        //cout<<x<<' '<<y<<"=="<<endl;
        if(vis[x][y]==cnt && arr[x][y]=='D')
            return INF;
        return 0;
    }
    vis[x][y]=cnt;

    int ans=0,t;

    if(arr[x][y]=='A')ans=1;

    for(int i=0;i<4;i++){
        int a=x+X[i],b=y+Y[i];
        char p=dim[next];
        //cout<<a<<' '<<b<<' '<<p<<endl;
        if(a<0 || a>=n || b<0 || b>=m || arr[a][b] != p)continue;

        t=go(a,b,(next+1)%4,cnt);
        if(t==INF)return t;
        ans+=t;
    }

    return ans;
}

int main(){
    //freopen("input.txt","r",stdin);
    int i,j,ans=0,cnt=1;
    cin>>n>>m;
    for(i=0;i<n;i++)
        cin>>arr[i];
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            if(!vis[i][j] && arr[i][j]=='D'){

                ans=max(ans,go(i,j,1,cnt));

                //if(i==0 && j==0)
                    //return 0;//cout<<t<<endl;
                if(ans==INF){
                    //cout<<i<<' '<<j<<endl;
                    return cout<<"Poor Inna!"<<endl,0;
                }
                cnt++;
            }

        }
    }
    if(ans==0)
        cout<<"Poor Dima!"<<endl;
    else cout<<ans<<endl;

    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;

#define MAX 1003
#define INF 100000008

string arr[MAX],dim="DIMA";
int n,m,vis[MAX][MAX],ANS[MAX][MAX];
int X[]={0,0,1,-1};
int Y[]={1,-1,0,0};

int go(int x,int y,int next){
    if(vis[x][y]){
        if(vis[x][y]==1)
            return INF;
        return ANS[x][y];
    }
    ANS[x][y]=1;
    vis[x][y]=1;

    int t=0;

    for(int i=0;i<4;i++){
        int a=x+X[i],b=y+Y[i];
        char p=dim[next];
        if(a<0 || a>=n || b<0 || b>=m || arr[a][b] != p)continue;

        t=max(t,go(a,b,(next+1)%4));
        if(t==INF)return t;
    }

    ANS[x][y]+=t;

    vis[x][y]=2;

    return ANS[x][y];
}

int main(){
    //freopen("input.txt","r",stdin);
    int i,j,ans=0;
    cin>>n>>m;
    for(i=0;i<n;i++)
        cin>>arr[i];
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            if(!vis[i][j] && arr[i][j]=='D'){

                ans=max(ans,go(i,j,1));
                if(ans==INF){
                    return cout<<"Poor Inna!"<<endl,0;
                }
            }

        }
    }
    if(ans<4)
        cout<<"Poor Dima!"<<endl;
    else cout<<ans/4<<endl;

    return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;

#define MAX 1003
#define INF 100000008

string arr[MAX],dim="DIMA";
int n,m,vis[MAX][MAX];
int X[]={0,0,1,-1};
int Y[]={1,-1,0,0};

int go(int x,int y,int next,int cnt){
    //cout<<x<<' '<<y<<' '<<next<<' '<<cnt<<endl;
    if(vis[x][y]){
        //cout<<x<<' '<<y<<"=="<<endl;
        if(vis[x][y]==cnt && arr[x][y]=='D')
            return INF;
        return 0;
    }
    vis[x][y]=cnt;

    int ans=0,t;

    if(arr[x][y]=='A')ans=1;

    for(int i=0;i<4;i++){
        int a=x+X[i],b=y+Y[i];
        char p=dim[next];
        //cout<<a<<' '<<b<<' '<<p<<endl;
        if(a<0 || a>=n || b<0 || b>=m || arr[a][b] != p)continue;

        t=go(a,b,(next+1)%4,cnt);
        if(t==INF)return t;
        ans+=t;
    }

    return ans;
}

int main(){
    //freopen("input.txt","r",stdin);
    int i,j,ans=0,cnt=1;
    cin>>n>>m;
    for(i=0;i<n;i++)
        cin>>arr[i];
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            if(!vis[i][j] && arr[i][j]=='D'){

                ans=max(ans,go(i,j,1,cnt));

                //if(i==0 && j==0)
                    //return 0;//cout<<t<<endl;
                if(ans==INF){
                    //cout<<i<<' '<<j<<endl;
                    return cout<<"Poor Inna!"<<endl,0;
                }
                cnt++;
            }

        }
    }
    if(ans==0)
        cout<<"Poor Inna!"<<endl;
    else cout<<ans<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

/*

  WTF :| !!

*/

vector<string>s;
vector<vector<int> > mem;
map<char, char>mp;
int n, m, vis[1003][1003], mrk = 2, ans;
int dx[] = {1, -1, 0, 0},
    dy[] = {0, 0, 1, -1};

bool cyc(int a, int b, int cnt) {
  if(s[a][b] == 'D' and vis[a][b] == mrk) {
    return 1;
  }
  if(s[a][b] == 'A') {
    cnt++;
    ans = max(ans, cnt);
  }
  bool ret = 0;
  vis[a][b] = mrk;
  for(int i = 0;i < 4;i++) {
    int x = a + dx[i], y = b + dy[i];
    if(x >= 0 and x < n and y >= 0 and y < m and mp[s[a][b]] == s[x][y]) {
      // vis[x][y] = mrk;
      ret |= cyc(x, y, cnt);
      // vis[x][y] = mrk - 1;
    }
  }
  vis[a][b] = mrk - 1;
  return ret;
}

int main() {
  mp['D'] = 'I';
  mp['I'] = 'M';
  mp['M'] = 'A';
  mp['A'] = 'D';
  cin >> n >> m;
  s.resize(n);
  mem.resize(n);
  for(int i = 0;i < n;i++) {
    cin >> s[i];
    mem[i].resize(m);
    for(int j = 0;j < m;j++) {
      mem[i][j] = 0;
    }
  }
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < m;j++) {
      if(s[i][j] == 'D' and !mem[i][j]) {
        if(cyc(i, j, 0)) {
          vis[i][j] = mrk;
          puts("Poor Inna!");
          return 0;
        }
        mrk++;
      }
    }
  }
  if(ans == 0) {
    puts("Poor Dima!");
  } else {
    cout << ans << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

vector<string>s;
vector<vector<int> > mem;
map<char, char>mp;
int ans, n, m, sx, sy;
int dx[] = {1, -1, 0, 0},
    dy[] = {0, 0, 1, -1};

bool cyc(int a, int b) {
  queue<int> qx, qy;
  qx.push(a);
  qy.push(b);
  mem[a][b] = 1;
  while(!qx.empty()) {
    int tx = qx.front(), ty = qy.front();
    qx.pop();
    qy.pop();
    // cout << tx << ' ' << ty << endl;
    for(int i = 0;i < 4;i++) {
      int x = tx + dx[i], y = ty + dy[i];

      if(x >= 0 and x < n and y >= 0 and y < m and mp[s[tx][ty]] == s[x][y]) {
        if(x == a and y == b and mem[x][y]) {
          // cout << "HERE " << x << ' ' << y << ' ' << a << ' ' << b << endl;
          return 1;
        } else if(!mem[x][y]) {
          mem[x][y] = 1;
          qx.push(x);
          qy.push(y);
        }
      }
    }
  }
  return 0;
}

int path(int a, int b) {
  stack<int>sx, sy;
  stack<int>qx, qy;
  sx.push(a);
  sy.push(b);
  qx.push(a);
  qy.push(b);
  mem[a][b] = 1;
  while(!qx.empty()) {
    int tx = qx.top(), ty = qy.top();
    qx.pop();
    qy.pop();
    for(int i = 0;i < 4;i++) {
      int x = tx + dx[i], y = ty + dy[i];
      if(x >= 0 and x < n and y >= 0 and y < m and mp[s[tx][ty]] == s[x][y] and mem[x][y] == -1) {
        mem[x][y] = 1;
        sx.push(x);
        sy.push(y);
        qx.push(x);
        qy.push(y);
      }
    }
  }
  while(!sx.empty()) {
    int tx = sx.top(), ty = sy.top(), ans = 1;
    // cout << "TST " << tx << ' ' << ty << endl;
    sx.pop();
    sy.pop();
    for(int i = 0;i < 4;i++) {
      int x = tx + dx[i], y = ty + dy[i];
      if(x >= 0 and x < n and y >= 0 and y < m and mp[s[tx][ty]] == s[x][y]) {
        ans = max(ans, mem[x][y] + 1);
      }
    }
    mem[tx][ty] = ans;
    // cout << ans << endl;
  }
  return mem[a][b];
}

int main() {
  mp['D'] = 'I';
  mp['I'] = 'M';
  mp['M'] = 'A';
  mp['A'] = 'D';
  cin >> n >> m;
  s.resize(n);
  mem.resize(n);
  for(int i = 0;i < n;i++) {
    cin >> s[i];
    mem[i].resize(m);
    for(int j = 0;j < m;j++) {
      mem[i][j] = 0;
    }
  }
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < m;j++) {
      if(s[i][j] == 'D' and !mem[i][j]) {
        if(cyc(i, j)) {
          puts("Poor Inna!");
          return 0;
        }
      }
    }
  }
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < m;j++) {
      mem[i][j] = -1;
    }
  }
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < m;j++) {
      // cout << mem[i][j] << ' ';
      if(s[i][j] == 'D' and mem[i][j] == -1) {
        ans = max(ans, path(i, j)/4);
        // cout << i << ' ' << j << ' ' << ans << endl;
      }
    }
    // cout << endl;
  }
  if(ans == 0) {
    puts("Poor Dima!");
  } else {
    cout << ans << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

string s[1003];
int mem[1003][1003];
map<char, char>mp;
int ans, n, m;
int dx[] = {1, -1, 0, 0},
    dy[] = {0, 0, 1, -1};

bool cyc(int a, int b, int sx, int sy) {
  if(mem[a][b] and a == sx and b == sy) {
    return 1;
  }
  if(mem[a][b])
    return 0;
  mem[a][b] = 1;
  bool ret = 0;
  for(int i = 0;i < 4;i++) {
    int x = a + dx[i], y = b + dy[i];
    if(x >= 0  and x < n and y >= 0 and y < m and s[x][y] == mp[s[a][b]]) {
      ret |= cyc(x, y, sx, sy);
    }
  }
  return ret;
}

int path(int a, int b) {
  if(mem[a][b] != -1) {
    return mem[a][b];
  }
  int ret = 1;
  for(int i = 0;i < 4;i++) {
    int x = a + dx[i], y = b + dy[i];
    if(x >= 0 and x < n and y >= 0 and y < m and mp[s[a][b]] == s[x][y]) {
      ret = max(ret, path(x, y) + 1);
    }
  }
  return mem[a][b] = ret;
}

int main() {
  mp['D'] = 'I';
  mp['I'] = 'M';
  mp['M'] = 'A';
  mp['A'] = 'D';
  cin >> n >> m;
  for(int i = 0;i < n;i++) {
    cin >> s[i];
  }
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < m;j++) {
      if(s[i][j] == 'D' and !mem[i][j]) {
        if(cyc(i, j, i, j)) {
          puts("Poor Inna!");
          return 0;
        }
      }
    }
  }
  memset(mem, -1, sizeof mem);
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < m;j++) {
      if(s[i][j] == 'D' and mem[i][j] == -1) {
        ans = max(ans, path(i, j) / 4);
        // cout << i << ' ' << j << ' ' << ans << endl;
      }
    }
  }
  if(ans == 0) {
    puts("Poor Dima!");
  } else {
    cout << ans << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

vector<string>s;
vector<vector<int> > mem;
map<char, char>mp;
int ans, n, m, sx, sy;
int dx[] = {1, -1, 0, 0},
    dy[] = {0, 0, 1, -1};

bool cyc(int a, int b) {
  queue<int> qx, qy;
  qx.push(a);
  qy.push(b);
  mem[a][b] = 1;
  while(!qx.empty()) {
    int tx = qx.front(), ty = qy.front();
    qx.pop();
    qy.pop();
    // cout << tx << ' ' << ty << endl;
    for(int i = 0;i < 4;i++) {
      int x = tx + dx[i], y = ty + dy[i];

      if(x >= 0 and x < n and y >= 0 and y < m and mp[s[tx][ty]] == s[x][y]) {
        if(x == a and y == b and mem[x][y]) {
          // cout << "HERE " << x << ' ' << y << ' ' << a << ' ' << b << endl;
          return 1;
        } else if(!mem[x][y]) {
          mem[x][y] = 1;
          qx.push(x);
          qy.push(y);
        }
      }
    }
  }
  return 0;
}

int path(int a, int b) {
  stack<int>sx, sy;
  stack<int>qx, qy;
  sx.push(a);
  sy.push(b);
  qx.push(a);
  qy.push(b);
  while(!qx.empty()) {
    int tx = qx.top(), ty = qy.top();
    mem[tx][ty] = 1;
    qx.pop();
    qy.pop();
    for(int i = 0;i < 4;i++) {
      int x = tx + dx[i], y = ty + dy[i];
      if(x >= 0 and x < n and y >= 0 and y < m and mp[s[tx][ty]] == s[x][y]) {
        if(mem[x][y] == -1) {
          mem[x][y] = 0;
          sx.push(x);
          sy.push(y);
          qx.push(x);
          qy.push(y);
        }
      }
    }
  }
  while(!sx.empty()) {
    int tx = sx.top(), ty = sy.top(), ans = 1;
    // cout << "TST " << tx << ' ' << ty << endl;
    sx.pop();
    sy.pop();
    for(int i = 0;i < 4;i++) {
      int x = tx + dx[i], y = ty + dy[i];
      if(x >= 0 and x < n and y >= 0 and y < m and mp[s[tx][ty]] == s[x][y]) {
        ans = max(ans, mem[x][y] + 1);
      }
    }
    mem[tx][ty] = ans;
    // cout << ans << endl;
  }
  return mem[a][b];
}

int main() {
  mp['D'] = 'I';
  mp['I'] = 'M';
  mp['M'] = 'A';
  mp['A'] = 'D';
  cin >> n >> m;
  s.resize(n);
  mem.resize(n);
  for(int i = 0;i < n;i++) {
    cin >> s[i];
    mem[i].resize(m);
    for(int j = 0;j < m;j++) {
      mem[i][j] = 0;
    }
  }
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < m;j++) {
      if(s[i][j] == 'D' and !mem[i][j]) {
        if(cyc(i, j)) {
          puts("Poor Inna!");
          return 0;
        }
      }
    }
  }
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < m;j++) {
      mem[i][j] = -1;
    }
  }
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < m;j++) {
      // cout << mem[i][j] << ' ';
      if(s[i][j] == 'D' and mem[i][j] == -1) {
        ans = max(ans, path(i, j)/4);
        // cout << i << ' ' << j << ' ' << ans << endl;
      }
    }
    // cout << endl;
  }
  if(ans == 0) {
    puts("Poor Dima!");
  } else {
    cout << ans << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

vector<string>s;
vector<vector<int> > mem;
map<char, char>mp;
int ans, n, m, sx, sy;
int dx[] = {1, -1, 0, 0},
    dy[] = {0, 0, 1, -1};

bool cyc(int a, int b) {
  queue<int> qx, qy;
  qx.push(a);
  qy.push(b);
  mem[a][b] = 1;
  while(!qx.empty()) {
    int tx = qx.front(), ty = qy.front();
    qx.pop();
    qy.pop();
    // cout << tx << ' ' << ty << endl;
    for(int i = 0;i < 4;i++) {
      int x = tx + dx[i], y = ty + dy[i];

      if(x >= 0 and x < n and y >= 0 and y < m and mp[s[tx][ty]] == s[x][y]) {
        if(x == a and y == b and mem[x][y]) {
          // cout << "HERE " << x << ' ' << y << ' ' << a << ' ' << b << endl;
          return 1;
        } else if(!mem[x][y]) {
          mem[x][y] = 1;
          qx.push(x);
          qy.push(y);
        }
      }
    }
  }
  return 0;
}

int path(int a, int b) {
  stack<int>sx, sy;
  stack<int>qx, qy;
  sx.push(a);
  sy.push(b);
  qx.push(a);
  qy.push(b);
  mem[a][b] = 1;
  while(!qx.empty()) {
    int tx = qx.top(), ty = qy.top();
    qx.pop();
    qy.pop();
    for(int i = 0;i < 4;i++) {
      int x = tx + dx[i], y = ty + dy[i];
      if(x >= 0 and x < n and y >= 0 and y < m and mp[s[tx][ty]] == s[x][y] and mem[x][y] == -1) {
        mem[x][y] = 0;
        sx.push(x);
        sy.push(y);
        qx.push(x);
        qy.push(y);
      }
    }
  }
  while(!sx.empty()) {
    int tx = sx.top(), ty = sy.top(), ans = 1;
    // cout << "TST " << tx << ' ' << ty << endl;
    sx.pop();
    sy.pop();
    for(int i = 0;i < 4;i++) {
      int x = tx + dx[i], y = ty + dy[i];
      if(x >= 0 and x < n and y >= 0 and y < m and mp[s[tx][ty]] == s[x][y]) {
        ans = max(ans, mem[x][y] + 1);
      }
    }
    mem[tx][ty] = ans;
    // cout << ans << endl;
  }
  return mem[a][b];
}

int main() {
  mp['D'] = 'I';
  mp['I'] = 'M';
  mp['M'] = 'A';
  mp['A'] = 'D';
  cin >> n >> m;
  s.resize(n);
  mem.resize(n);
  for(int i = 0;i < n;i++) {
    cin >> s[i];
    mem[i].resize(m);
    for(int j = 0;j < m;j++) {
      mem[i][j] = 0;
    }
  }
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < m;j++) {
      if(s[i][j] == 'D' and !mem[i][j]) {
        if(cyc(i, j)) {
          puts("Poor Inna!");
          return 0;
        }
      }
    }
  }
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < m;j++) {
      mem[i][j] = -1;
    }
  }
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < m;j++) {
      // cout << mem[i][j] << ' ';
      if(s[i][j] == 'D' and mem[i][j] != 1) {
        ans = max(ans, path(i, j)/4);
        // cout << i << ' ' << j << ' ' << ans << endl;
      }
    }
    // cout << endl;
  }
  if(ans == 0) {
    puts("Poor Dima!");
  } else {
    cout << ans << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

string s[1003];
int mem[1003][1003];
map<char, char>mp;
int ans, n, m;
int dx[] = {1, -1, 0, 0},
    dy[] = {0, 0, 1, -1};

bool cyc(int a, int b) {
  mem[a][b] = 1;
  bool ret = 0;
  for(int i = 0;i < 4;i++) {
    int x = a + dx[i], y = b + dy[i];
    if(x >= 0  and x < n and y >= 0 and y < m and s[x][y] == mp[s[a][b]]) {
      if(mem[x][y] and s[a][b] == 'A') {
        return 1;
      } else if(!mem[x][y]){
        ret |= cyc(x, y);
      }
    }
  }
  return ret;
}

int path(int a, int b) {
  if(mem[a][b] != -1) {
    return mem[a][b];
  }
  int ret = 1;
  for(int i = 0;i < 4;i++) {
    int x = a + dx[i], y = b + dy[i];
    if(x >= 0 and x < n and y >= 0 and y < m and mp[s[a][b]] == s[x][y]) {
      ret = max(ret, path(x, y) + 1);
    }
  }
  return mem[a][b] = ret;
}

int main() {
  mp['D'] = 'I';
  mp['I'] = 'M';
  mp['M'] = 'A';
  mp['A'] = 'D';
  cin >> n >> m;
  for(int i = 0;i < n;i++) {
    cin >> s[i];
  }
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < m;j++) {
      if(s[i][j] == 'D' and !mem[i][j]) {
        if(cyc(i, j)) {
          puts("Poor Inna!");
          return 0;
        }
      }
    }
  }
  memset(mem, -1, sizeof mem);
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < m;j++) {
      if(s[i][j] == 'D' and mem[i][j] == -1) {
        ans = max(ans, path(i, j) / 4);
        // cout << i << ' ' << j << ' ' << ans << endl;
      }
    }
  }
  if(ans == 0) {
    puts("Poor Dima!");
  } else {
    cout << ans << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

/*

  WTF :| !!

*/

vector<string>s;
vector<vector<int> > mem;
map<char, char>mp;
int n, m, vis[1003][1003], ans;
int dx[] = {1, -1, 0, 0},
    dy[] = {0, 0, 1, -1};

bool cyc(int a, int b, int cnt) {
  if(s[a][b] == 'D' and vis[a][b] == 1) {
    return 1;
  }
  if(s[a][b] == 'A') {
    cnt++;
    ans = max(ans, cnt);
  }
  bool ret = 0;
  vis[a][b] = 1;
  for(int i = 0;i < 4;i++) {
    int x = a + dx[i], y = b + dy[i];
    if(x >= 0 and x < n and y >= 0 and y < m and mp[s[a][b]] == s[x][y] and vis[x][y] != 2) {
      ret |= cyc(x, y, cnt);
    }
  }
  vis[a][b] = 2;
  return ret;
}

int main() {
  mp['D'] = 'I';
  mp['I'] = 'M';
  mp['M'] = 'A';
  mp['A'] = 'D';
  cin >> n >> m;
  s.resize(n);
  mem.resize(n);
  for(int i = 0;i < n;i++) {
    cin >> s[i];
    mem[i].resize(m);
    for(int j = 0;j < m;j++) {
      mem[i][j] = 0;
    }
  }
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < m;j++) {
      if(s[i][j] == 'D' and !mem[i][j]) {
        if(cyc(i, j, 0)) {
          puts("Poor Inna!");
          return 0;
        }
      }
    }
  }
  if(ans == 0) {
    puts("Poor Dima!");
  } else {
    cout << ans << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

vector<string>s;
vector<vector<int> > mem;
map<char, char>mp;
int ans, n, m;
int dx[] = {1, -1, 0, 0},
    dy[] = {0, 0, 1, -1};

bool cyc(int a, int b, int sx, int sy) {
  if(s[a][b] == 'D' and a == sx and b == sy) {
    return 1;
  }
  if(s[a][b] == 'D') {
    sx = a;
    sy = b;
  }
  bool ret = 0;
  for(int i = 0;i < 4;i++) {
    int x = a + dx[i], y = b + dy[i];
    if(x >= 0 and x < n and y >= 0 and y < m and mp[s[a][b]] == s[x][y]) {
      ret |= cyc(x, y, sx, sy);
    }
  }
  return ret;
}

int path(int a, int b) {
  if(mem[a][b] != -1) {
    return mem[a][b];
  }
  int ret = 1;
  for(int i = 0;i < 4;i++) {
    int x = a + dx[i], y = b + dy[i];
    if(x >= 0 and x < n and y >= 0 and y < m and mp[s[a][b]] == s[x][y]) {
      int tmp = mem[x][y];
      if(tmp == -1)
        ret = max(ret, path(x, y) + 1);
      else
        ret = max(ret, tmp + 1);
    }
  }
  return mem[a][b] = ret;
}

int main() {
  mp['D'] = 'I';
  mp['I'] = 'M';
  mp['M'] = 'A';
  mp['A'] = 'D';
  cin >> n >> m;
  s.resize(n);
  mem.resize(n);
  for(int i = 0;i < n;i++) {
    cin >> s[i];
    mem[i].resize(m);
    for(int j = 0;j < m;j++) {
      mem[i][j] = 0;
    }
  }
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < m;j++) {
      if(s[i][j] == 'D' and !mem[i][j]) {
        if(cyc(i, j, -1, -1)) {
          puts("Poor Inna!");
          return 0;
        }
      }
    }
  }
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < m;j++) {
      mem[i][j] = -1;
    }
  }
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < m;j++) {
      if(s[i][j] == 'D' and mem[i][j] == -1) {
        ans = max(ans, path(i, j) / 4);
      }
    }
  }
  if(ans == 0) {
    puts("Poor Dima!");
  } else {
    cout << ans << endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

/*

  WTF :| !!

*/

vector<string>s;
vector<vector<int> > mem;
map<char, char>mp;
int n, m, vis[1003][1003], mrk = 2, ans;
int dx[] = {1, -1, 0, 0},
    dy[] = {0, 0, 1, -1};

bool cyc(int a, int b, int cnt) {
  if(s[a][b] == 'D' and vis[a][b] == mrk) {
    return 1;
  }
  if(s[a][b] == 'A') {
    cnt++;
    ans = max(ans, cnt);
  }
  bool ret = 0;
  vis[a][b] = mrk;
  for(int i = 0;i < 4;i++) {
    int x = a + dx[i], y = b + dy[i];
    if(x >= 0 and x < n and y >= 0 and y < m and mp[s[a][b]] == s[x][y] and vis[x][y] != mrk - 1) {
      // vis[x][y] = mrk;
      ret |= cyc(x, y, cnt);
      // vis[x][y] = mrk - 1;
    }
  }
  vis[a][b] = mrk - 1;
  return ret;
}

int main() {
  mp['D'] = 'I';
  mp['I'] = 'M';
  mp['M'] = 'A';
  mp['A'] = 'D';
  cin >> n >> m;
  s.resize(n);
  mem.resize(n);
  for(int i = 0;i < n;i++) {
    cin >> s[i];
    mem[i].resize(m);
    for(int j = 0;j < m;j++) {
      mem[i][j] = 0;
    }
  }
  for(int i = 0;i < n;i++) {
    for(int j = 0;j < m;j++) {
      if(s[i][j] == 'D' and !mem[i][j]) {
        if(cyc(i, j, 0)) {
          vis[i][j] = mrk;
          puts("Poor Inna!");
          return 0;
        }
        mrk++;
      }
    }
  }
  if(ans == 0) {
    puts("Poor Dima!");
  } else {
    cout << ans << endl;
  }
  return 0;
}